重复向量直到第 n 个元素
repeat a vector up to the nth element
我要重复的向量是:
> months2014
[1] "07" "08" "09" "10" "11" "12"
我想根据这个定义第 n 个值的向量重复它:
> num_times
[1] 6 6 6 6 5 6 6 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
[33] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 12 6 6 6 6 6 6 6 12 6 6 6 6 6 6
[65] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 12 6
[97] 6 6 12 6 6 6 6 6 6 6 6 12 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 2 6 6
[129] 6 12 6 6 6 6 6 6
我使用这个循环得到以下输出:
for (i in num_times)
print(months2014[1:i])
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" ## repeated upto the 5th element
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" ## repeated up to the 6th element
如果你查看 num_times 向量,它也有 12。因此我也得到了这个结果:
[1] "07" "08" "09" "10" "11" "12" NA NA NA NA NA NA
months2014
中只有 6 个元素,所以我得到了 NA。我实际上希望它重新启动并获得如下输出:
[1] "07" "08" "09" "10" "11" "12" "07 "08" "09" "10" "11" "12"
我有三个问题:
1) 如何保存循环输出?我尝试创建一个空向量并将结果放入其中,但这没有用:
temp_vec <- c()
for (i in num_times)
temp_vec <- print(months2014[1:i])
2) 如何修复 NA?
3) 一定有更好的方法吗?
经过测试这似乎有效:
sapply(num_times,function(x) rep(months2014,length.out=x)
如果num_times
中的值大于months214
的长度,则输出一个列表并重复序列:
[[1]]
[1] "07" "08" "09" "10" "11" "12"
[[2]]
[1] "07" "08" "09" "10" "11" "12"
[[3]]
[1] "07" "08" "09" "10" "11" "12"
[[4]]
[1] "07" "08" "09" "10" "11" "12"
[[5]]
[1] "07" "08" "09" "10" "11"
[[6]]
[1] "07" "08" "09" "10" "11" "12" "07" "08" "09" "10" "11" "12"
我要重复的向量是:
> months2014
[1] "07" "08" "09" "10" "11" "12"
我想根据这个定义第 n 个值的向量重复它:
> num_times
[1] 6 6 6 6 5 6 6 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
[33] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 12 6 6 6 6 6 6 6 12 6 6 6 6 6 6
[65] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 12 6
[97] 6 6 12 6 6 6 6 6 6 6 6 12 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 2 6 6
[129] 6 12 6 6 6 6 6 6
我使用这个循环得到以下输出:
for (i in num_times)
print(months2014[1:i])
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" ## repeated upto the 5th element
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" "12"
[1] "07" "08" "09" "10" "11" ## repeated up to the 6th element
如果你查看 num_times 向量,它也有 12。因此我也得到了这个结果:
[1] "07" "08" "09" "10" "11" "12" NA NA NA NA NA NA
months2014
中只有 6 个元素,所以我得到了 NA。我实际上希望它重新启动并获得如下输出:
[1] "07" "08" "09" "10" "11" "12" "07 "08" "09" "10" "11" "12"
我有三个问题:
1) 如何保存循环输出?我尝试创建一个空向量并将结果放入其中,但这没有用:
temp_vec <- c()
for (i in num_times)
temp_vec <- print(months2014[1:i])
2) 如何修复 NA?
3) 一定有更好的方法吗?
经过测试这似乎有效:
sapply(num_times,function(x) rep(months2014,length.out=x)
如果num_times
中的值大于months214
的长度,则输出一个列表并重复序列:
[[1]]
[1] "07" "08" "09" "10" "11" "12"
[[2]]
[1] "07" "08" "09" "10" "11" "12"
[[3]]
[1] "07" "08" "09" "10" "11" "12"
[[4]]
[1] "07" "08" "09" "10" "11" "12"
[[5]]
[1] "07" "08" "09" "10" "11"
[[6]]
[1] "07" "08" "09" "10" "11" "12" "07" "08" "09" "10" "11" "12"