Skip to content
Prev 59524 / 398502 Next

RE : Create sequence for dataset

ssim at lic.co.nz writes:
Whoa. Who just said that SAS data step code was clearer than R? Quite
a bit of implicit knowledge in that one.

Here's one way (someone please think up a better name for ave()...):
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
 [19] 19 20 21 22 23 24 25 26 27 28 29 30 31  1  2  3  4  5
 [37]  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 [55] 24 25 26 27 28 29 30  1  2  3  4  5  6  7  8  9 10 11
 [73] 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 [91] 30 31  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
[109] 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  1  2  3
[127]  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21
[145] 22 23 24 25 26 27 28 29 30

or, same basic idea but a little less cryptic:
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
 [19] 19 20 21 22 23 24 25 26 27 28 29 30 31  1  2  3  4  5
(etc.)

or, brute force and ignorance:
+   ix <- airquality$Month == i
+   x[ix] <- seq(along=x[ix])
+ }
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
 [19] 19 20 21 22 23 24 25 26 27 28 29 30 31  1  2  3  4  5
....

or, going to the opposite extreme (Gabor et al. are going to try and
beat me on this...):
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
 [19] 19 20 21 22 23 24 25 26 27 28 29 30 31  1  2  3  4  5
....