I have a data frame on which I calculate a run length encoding for a specific column. The values of the column, dir
, are either -1, 0, or 1.
dir.rle <- rle(df$dir)
I then take the run lengths and compute segmented cumulative sums across another column in the data frame. I'm using a for loop, but I feel like there should be a way to do this more intelligently.
ndx <- 1
for(i in 1:length(dir.rle$lengths)) {
l <- dir.rle$lengths[i] - 1
s <- ndx
e <- ndx+l
tmp[s:e,]$cumval <- cumsum(df[s:e,]$val)
ndx <- e + 1
}
The run lengths of dir
define the start, s
, and end, e
, for each run. The above code works but it does not feel like idiomatic R code. I feel as if there should be another way to do it without the loop.