Cumulative sums over run lengths. Can this loop be vectorized?

后端 未结 3 1133
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 10:46

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.r

3条回答
  •  时光说笑
    2020-12-18 11:06

    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)
    

提交回复
热议问题