I'm trying to do simulation of a sequence, which is of length N (varies between 10k and 3M) represented by a vector which has n 1's and s 0's where N = n+s.
I'd like to reduce this to a vector on the form c( 137, 278, 21271, 124162, ... ) where the numbers are the number of consecutive 1's in the original vector. Since I need to do this ~100,000 times for the simulation I'm doing I'm looking for as efficient a method as possible!
Thanks!
Martin
you can use rle
to get that
x <- sample(c(1, 0), size = 3e+06, replace = TRUE)
x.rle <- rle(x)
x.rle
## Run Length Encoding
## lengths: int [1:1499270] 4 1 2 3 4 1 1 3 1 4 ...
## values : num [1:1499270] 0 1 0 1 0 1 0 1 0 1 ...
vectorOf1 <- x.rle$lengths[x.rle$values == 1]
vectorOf2 <- x.rle$lengths[x.rle$values == 0]
head(vectorOf1, 20)
## [1] 1 3 1 3 4 3 1 1 1 4 4 2 3 1 1 4 1 1 1 1
head(vectorOf2, 20)
## [1] 4 2 4 1 1 1 1 5 2 2 2 1 3 3 7 2 1 1 1 2