Holly
Holly

Reputation: 1

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.

Upvotes: 0

Views: 28

Answers (1)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

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

Upvotes: 1

Related Questions