I am trying to divide up (not necessarily into even chunks, bc the real data may vary) a single column of integers called scores (.csv file) and then count the consecutive values (of x chosen value, e.g. 1) in each divided portion or the mean length of a consecutive value. All possible with rle.
I can easily split the column of integers using split however this is seemingly incompatible with rle (presumably bc split generates a list). I looked for solutions and/or alternatives to rle but didn't come up with anything.
Example Scores
scores <- c(1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1)
Split them
g <- seq_along(scores)
scores.div <- split(scores, ceiling(g/7))
Example of what I tried, but didn't work
Scores.rle <- sapply(scores.div, function(x) {
r <- rle(x)
sum(r$values == 1)
})
I'd expect some output like this:
2 2 0 1 1
Any help is greatly appreciated