Home > Enterprise >  How to sum values in a column based on other column(s) in R
How to sum values in a column based on other column(s) in R

Time:08-18

I have the following dataset:

Geography Sex Year Population
AB F 2000 45320
AB M 2000 42220
ON F 2001 51230
ON M 2001 51422

I want to sum the populations by province and year, where for example, I would get 46752 as the population of AB in the year 2000.

I have tried using dplyr and summarise, but keep getting stuck.

This is what I had so far:

count_province <- df %>% group_by(Geography, Year, Population) %>% summarise(
  prov_pop = sum(Population))

CodePudding user response:

In the group_by, remove the Population

library(dplyr)
df %>%
   group_by(Geography, Year) %>%
   summarise(prov_pop = sum(as.numeric(Population), na.rm = TRUE))
  •  Tags:  
  • r
  • Related