简体   繁体   中英

How to Sum a column of data based on their group

I'm extremely new to R, I am being assessed on it but really struggling to get my head around it, so sorry if I use the wrong jargon etc.

I have been provided a table of data, in which contains columns I am particularly looking at: 'Country','Continent' and 'People_Fully_Vaccinated'. In this data, I want to sum the number of 'People_Fully_Vaccinated' based on their continent to convert into a graph, however I am having trouble grouping countries based on their continent to combine the number of people vaccinated. It seems my columns are not being recognised as objects but I'm not sure how to assign a full column of numbers to an object.

I'm sorry if I've written this in a confusing way- any help would be much appreciated.

How about this dplyr method?

Toy data:

df <- data.frame(
  Continent = c("A", "A", "B", "B"),
  Ppl_fully_vaccinated = c(1000, 2555, 33, 44)
)

Method:

library(dplyr)
df %>% 
  group_by(Continent) %>%
  summarise(Total = sum(Ppl_fully_vaccinated))
# A tibble: 2 × 2
  Continent Total
  <chr>     <dbl>
1 A          3555
2 B            77

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM