本文介绍了生成具有重复间隔的数字序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建 6 个案例的序列,但间隔为 144 个案例.
I am trying to create sequences of number of 6 cases, but with 144 cases intervals.
以这个为例
c(1:6, 144:149, 288:293)
1 2 3 4 5 6 144 145 146 147 148 149 288 289 290 291 292 293
如何使用
seq
或使用其他功能?
推荐答案
我发现 sequence
函数在这种情况下很有帮助.如果您的数据采用这样的结构:
I find the sequence
function to be helpful in this case. If you had your data in a structure like this:
(info <- data.frame(start=c(1, 144, 288), len=c(6, 6, 6)))
# start len
# 1 1 6
# 2 144 6
# 3 288 6
那么你可以在一行中做到这一点:
then you could do this in one line with:
sequence(info$len) + rep(info$start-1, info$len)
# [1] 1 2 3 4 5 6 144 145 146 147 148 149 288 289 290 291 292 293
请注意,即使您组合的序列长度不同,此解决方案也有效.
Note that this solution works even if the sequences you're combining are different lengths.
这篇关于生成具有重复间隔的数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!