0

I am trying to group some rows summing the values of one of the columns. In the example I want to sum votes by districts and parties. My goal is to group municipalities and get party votes within a district. I guess it is very easy but my attempts with aggregate and group_by have not worked so far. Thank you!

df <- structure(list(municip = c("A", "A", "A", "B", "B", "C", "C"), 
                               district = c("one", "one", "one", "one", "one", "two", "two"
                               ), party = c("green", "blue", "red", "green", "blue", "green", 
                                            "blue"), votes = c(10, 20, 10, 40, 30, 20, 10)), row.names = c(NA, 
                                                                                                           -7L), class = c("tbl_df", "tbl", "data.frame"))

enter image description here

Jasper
  • 95
  • 6
  • I think you need `df %>% group_by(district, party) %>% summarise(votes = sum(votes), .groups = "drop")`. – Martin Gal Aug 20 '21 at 10:24
  • Does this answer your question? [How to select the rows with maximum values in each group with dplyr?](https://stackoverflow.com/questions/24237399/how-to-select-the-rows-with-maximum-values-in-each-group-with-dplyr) – andschar Aug 20 '21 at 10:27
  • 1
    @andschar That doesn't seem related to what the OP is asking. – Matteo Aug 20 '21 at 10:28
  • 1
    Thanks @matteo. Oops, I meant: https://stackoverflow.com/questions/25314336/extract-the-maximum-value-within-each-group-in-a-dataframe – andschar Aug 20 '21 at 10:31
  • @MartinGal thanks a lot, it works. Do you know if there is a way to keep in the new dataframe other columns, unchanged or using `sum()`? – Jasper Aug 20 '21 at 13:31

0 Answers0