I would like to group common values in a column, and obtain sums of the corresponding values in other columns. I have the following data:
df <- data.frame(age = c("40-45", "40-45", "40-45", "40-45"),
age.cat = c("35-40", "40-45", "35-40", "40-45"),
x = c(21, 170, 36, 96),
y = c(47, 110, 121, 394),
desc = c("eating", "eating", "running", "running"))
age age.cat x y desc
40-45 35-40 21 47 eating
40-45 40-45 170 110 eating
40-45 35-40 36 121 running
40-45 40-45 96 394 running
For every instance that age.cat and age don't match, I'd like to convert age.cat into age and condense that row by summing the values in x and y. So the output will look like:
age age.cat x y desc
40-45 40-45 191 157 eating
40-45 40-45 132 515 running
This question is very obviously not a duplicate of this question, since I am also asking to retain the age.cat variable. Notice from the desired output that I am asking to group age and also retain age.cat when age is equal to age.cat.
How do I do this? A tidyverse solution is preferrable, thanks!