0

I want group by NAME and sum MONEY, but I want to keep the other cols of MONEY. When I did it, my result is a DF with just NAME and MONEY

  money = money %>%
  group_by(NAME) %>%
  summarise(SUM_MONEY = sum(MONEY))
jpsmith
  • 11,023
  • 5
  • 15
  • 36
  • In case of `sum` a grouped `summarise` will create one row with the sum of `MONEY` for each `NAME`. How would you like the other columns to be summarized? Do you also want to take the `sum` of those values? Then there is `across(your_column_selection_goes_here, sum)`. In any case it would be great to have a small example with your data and expected output. – TimTeaFan Aug 01 '23 at 14:18
  • 2
    Or if you don't want to collapse your data to one row per group, use `mutate()` instead of `summarise()`. – Gregor Thomas Aug 01 '23 at 14:21
  • @TimTeaFan I want keep the value of the other columns to each name. So I don't want to do nothing with them. – Lucas Esteves Aug 01 '23 at 14:22
  • 3
    seems like you want `mutate` and not `summarize`. Unless you can group by them, you caanot keep whole columns with aggregated/summarized values. The two will have different number of observations/rows – Onyambu Aug 01 '23 at 14:26
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 01 '23 at 14:35

0 Answers0