Question

Possible Duplicate:
Sequence of Repeated Values in R

I'm trying to generate an increasing sequence in R, with a total length of 5952. I have been writing it out by hand but I suspect there must be an easier way to do it. Here is the example:

v<- c(rep(1,96),rep(2,96),rep(3,96),rep(4,96), .... , rep(n,96)

Where n = 62. The result is a vector 5952 elements in length. One approach I thought might be useful is:

v = rep(NA, 96*62)
for(i in 1:62) {
v = c(rep(i,96))
} 

Of course, this doesn't work, the result is just v = c(rep(62,96)). Any tips?

Was it helpful?

Solution

Try:

rep(1:62,each=96)

length(rep(1:62,each=96))
[1] 5952
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top