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
dir.r
Add a 'group' column to the data frame. Something like:
df=data.frame(z=rnorm(100)) # dummy data df$dir = sign(df$z) # dummy +/- 1 rl = rle(df$dir) df$group = rep(1:length(rl$lengths),times=rl$lengths)
then use tapply to sum within groups:
tapply(df$z,df$group,sum)