0

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!

user438383
  • 5,716
  • 8
  • 28
  • 43
Cloft X
  • 141
  • 7
  • 1
    with this data, you could do a sum by group `df %>% summarise(across(x:y, sum), .by = c(desc, age))` – Maël Jul 19 '23 at 10:31
  • 1
    If what you need is as @Maël has said, then this issue has nothing to do with `age.cat`. The logic you describe on `age` and `age.cat` is unclear. – Darren Tsai Jul 19 '23 at 10:43
  • What @Maël proposed solves half my problem. What you can easily observe from the output desired is to group `age`, as well as retain `age.cat` when the two are equal. This should make it very clear to you. – Cloft X Jul 19 '23 at 11:06

0 Answers0