简体   繁体   中英

Creating R Sequence for 4 numbers increasing consecutively from different starting points

I am experienced in R, however have little to no experience in sequences.

I would like to make a sequence of numbers where the first 4 numbers are starting points, and increase consecutively. Namely:

5,72,144,210,[now I repeat each +1], 6,73,145,211,[and so on],7,74,146,212

Hence:

c(5,72,144,210,6,73,145,211,7,74,146,212..)

Would greatly appreciate any ideas on how to set this up. I would for example need this to continue on for 60 iterations.

Something like:

x <- c(5,72,144,210)
rep(x, 60) + rep(0:59, each=length(x))

Or making use of R recycling as mentioned by Ronak Shah:

x + rep(0:59, each=length(x))

Or:

as.vector(do.call(rbind, lapply(c(5,72,144,210), function(k) k:(k+60))))

output:

  [1]   5  72 144 210   6  73 145 211   7  74 146 212   8  75 147 213   9  76 148 214  10  77 149 215
 [25]  11  78 150 216  12  79 151 217  13  80 152 218  14  81 153 219  15  82 154 220  16  83 155 221
 [49]  17  84 156 222  18  85 157 223  19  86 158 224  20  87 159 225  21  88 160 226  22  89 161 227
 [73]  23  90 162 228  24  91 163 229  25  92 164 230  26  93 165 231  27  94 166 232  28  95 167 233
 [97]  29  96 168 234  30  97 169 235  31  98 170 236  32  99 171 237  33 100 172 238  34 101 173 239
[121]  35 102 174 240  36 103 175 241  37 104 176 242  38 105 177 243  39 106 178 244  40 107 179 245
[145]  41 108 180 246  42 109 181 247  43 110 182 248  44 111 183 249  45 112 184 250  46 113 185 251
[169]  47 114 186 252  48 115 187 253  49 116 188 254  50 117 189 255  51 118 190 256  52 119 191 257
[193]  53 120 192 258  54 121 193 259  55 122 194 260  56 123 195 261  57 124 196 262  58 125 197 263
[217]  59 126 198 264  60 127 199 265  61 128 200 266  62 129 201 267  63 130 202 268  64 131 203 269

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM