R - Generate a sequence of numbers

giac picture giac · Jun 29, 2015 · Viewed 29.4k times · Source

I am trying to create sequences of number of 6 cases, but with 144 cases intervals.

Like this one for example

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

How could I generate automatically such a sequence with

seq 

or with another function ?

Answer

josliber picture josliber · Jun 29, 2015

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.