Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I summarize by group by in (R)?

I would like to summarize Votes by Party.

My table:


State     Party    Votes    

NY         RP       80
NY         DM       20
CA         RP       30
CA         DM       70


Expected:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Party   Votes

RP      110
DM       90


This doesnt work for me:

data <- data %>% group_by(Party) %>% summarise(Votos)

Thanks!

>Solution :

Use the sum() function within summarise

library(dplyr)
df %>% 
  group_by(Party) %>% 
  summarise(Votes = sum(Votes, na.rm = TRUE))
 Party Votes
  <chr> <int>
1 DM       90
2 RP      110

If you want to have sorted:

df %>% 
  group_by(Party) %>% 
  summarise(Votes = sum(Votes, na.rm = TRUE)) %>% 
  arrange(desc(Votes))
  Party Votes
  <chr> <int>
1 RP      110
2 DM       90
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading