Thomas Brüggen
Thomas Brüggen

Reputation: 21

How to create and add multiple sequences?

I want to create a sequence from 1 - 1000, in which I use the first 12 numbers, skip the next 12, and then take the next 12 again and so on (1:12,25:36,49:60 ...).

Does anyone know how to make this work?

Upvotes: 2

Views: 900

Answers (4)

DeBARtha
DeBARtha

Reputation: 458

Another method:

a<-seq(1,1000,by=24): This stores the value of your starting points for each consecutive sequence.

Then you can use sapply:

sapply(a,function(x) seq(x,x+11))

This would generate:

    #Sample output:
    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
 [1,]    1   25   49   73   97  121  145  169  193   217   241   265   289
 [2,]    2   26   50   74   98  122  146  170  194   218   242   266   290
 [3,]    3   27   51   75   99  123  147  171  195   219   243   267   291
 [4,]    4   28   52   76  100  124  148  172  196   220   244   268   292
 [5,]    5   29   53   77  101  125  149  173  197   221   245   269   293
 [6,]    6   30   54   78  102  126  150  174  198   222   246   270   294
 [7,]    7   31   55   79  103  127  151  175  199   223   247   271   295
 [8,]    8   32   56   80  104  128  152  176  200   224   248   272   296
 [9,]    9   33   57   81  105  129  153  177  201   225   249   273   297
[10,]   10   34   58   82  106  130  154  178  202   226   250   274   298
[11,]   11   35   59   83  107  131  155  179  203   227   251   275   299
[12,]   12   36   60   84  108  132  156  180  204   228   252   276   300

However, if you want to use a for-loop:

b=NULL
for(i in 1:length(a))
{
    b=c(b,seq(a[i],a[i]+11))
}

Now you have b as:

 #Sample of b
 [1]   1   2   3   4   5   6   7   8   9  10  11  12  25  26  27  28  29  30
 [19]  31  32  33  34  35  36  49  50  51  52  53  54  55  56  57  58  59  60
 [37]  73  74  75  76  77  78  79  80  81  82  83  84  97  98  99 100 101 102
 [55] 103 104 105 106 107 108 121 122 123 124 125 126 127 128 129 130 131 132
 [73] 145 146 147 148 149 150 151 152 153 154 155 156 169 170 171 172 173 174

However, this method is not so efficient as for loops are somewhat slow in R(compared to vectorised methods).

Upvotes: 0

Tech Commodities
Tech Commodities

Reputation: 1959

@StupidWolf has the simplest answer, but a function gives flexibility for any alternative sequences?

buildList <- function(seq = 12, skip = seq*2, max = 1000) {
  seed <- 1:seq
  out <- seed
  
  while(max(seed) < max){
    seed <- seed + skip
    out <- c(out, seed)
  }
  
  return(out[out <= max])
}

Specify the sequence of numbers, the number to skip and the maximum value.

buildList() will give your answer.

or buildList(seq = 4, max = 20) gives [1] 1 2 3 4 9 10 11 12 17 18 19 20

Upvotes: 0

coffeinjunky
coffeinjunky

Reputation: 11514

Try with an iterator of sorts, where the first call issues 1:12, the second call returns 25:36, etc. And then you call it 42 times (1000/12/2) to get to 1000:

wrap <- function(){
  i = 0
  myseq <- function(){
    out = 1:12 + i*12
    i <<- i+2
    return(out)
  }
  return(myseq)
}
myseq = wrap()

sapply(1:42, function(x) myseq())

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
 [1,]    1   25   49   73   97  121  145  169  193   217   241   265   289   313   337
 [2,]    2   26   50   74   98  122  146  170  194   218   242   266   290   314   338
 [3,]    3   27   51   75   99  123  147  171  195   219   243   267   291   315   339
 [4,]    4   28   52   76  100  124  148  172  196   220   244   268   292   316   340
 [5,]    5   29   53   77  101  125  149  173  197   221   245   269   293   317   341
 [6,]    6   30   54   78  102  126  150  174  198   222   246   270   294   318   342
 [7,]    7   31   55   79  103  127  151  175  199   223   247   271   295   319   343
 [8,]    8   32   56   80  104  128  152  176  200   224   248   272   296   320   344
 [9,]    9   33   57   81  105  129  153  177  201   225   249   273   297   321   345
[10,]   10   34   58   82  106  130  154  178  202   226   250   274   298   322   346
[11,]   11   35   59   83  107  131  155  179  203   227   251   275   299   323   347
[12,]   12   36   60   84  108  132  156  180  204   228   252   276   300   324   348

Upvotes: -1

StupidWolf
StupidWolf

Reputation: 46948

Very quickly (maybe stupid way), find the quotient of your series with an offset, use this as an index:

ix = (1:1000 - 1) %/% 12

You can see this groups your numbers by 12:

head(ix,25)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2

To get alternate, take only ix which are divisible by 2:

 head((1:1000)[ix %% 2 == 0],50)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 25 26 27 28 29 30 31 32 33 34 35 36 49
[26] 50 51 52 53 54 55 56 57 58 59 60 73 74 75 76 77 78 79 80 81 82 83 84 97 98

Upvotes: 2

Related Questions