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