Sometimes one has a number of variables with the same grouping, in particular as a result of doing a gather
on some variables, ex:
x0 x1 x2 variable value
1 1 Male Green 1 0.1803306
2 1 Male Green 2 0.5619410
3 1 Male Green 3 0.9905186
4 2 Female Blue 1 0.1549419
5 2 Female Blue 2 0.6917326
6 2 Female Blue 3 0.6509738
In such a case, I'd like to compute a grouped summary statistic (say, group_by(x0) %>% summarize(sum(value))
) while preserving all the ID variables given by the first columns. One way is to do group_by(x0, x1, x2)
but this becomes a little messy if there are a large number of ID variables, and group_by doesn't seem to work with the functions from select
, so I can't do group_by(starts_with("x"))
. How can I cleanly preserve all my ID variables post-summarize without typing out each variable name individually?