Use rle to group by runs when using dplyr

前端 未结 2 1892
庸人自扰
庸人自扰 2020-11-29 09:21

In R, I want to summarize my data after grouping it based on the runs of a variable x (aka each group of the data corresponds to a subset of the data where cons

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 10:06

    If you explicitly create a grouping variable g it more or less works:

    > dat %>% transform(g=with(rle(dat$x),{ rep(seq_along(lengths), lengths)}))%>%                                   
     group_by(g) %>% summarize(mean(y))
    Source: local data frame [4 x 2]
    
          g mean(y)
      (int)   (dbl)
    1     1     2.0
    2     2     4.5
    3     3     6.0
    4     4     7.0
    

    I used transform here because mutate throws an error.

提交回复
热议问题