Home > other >  Sum columns by year
Sum columns by year

Time:11-20

How Do I sum up all the values in 'exports' and 'imports' of all countries by year?

data frame :

Country Year exports imports
Denmark 2004 10000000 10000000
Denmark 2008 20000000 20000000
Denmark 2009 30000000 30000000
Norway 2004 10000000 10000000
Norway 2008 20000000 20000000
Norway 2009 10000000 30000000

I tried:

df_frame %>% 
  group_by(Year) %>% 
  summarize(
    total_exports = sum(exports),
    total_imports = sum(imports)
  )

But I got:

Year exports imports
2004 NA NA
2008 NA NA
2009 NA NA

I want:

Year exports imports
2004 40000000 20000000
2008 40000000 40000000
2009 20000000 60000000

CodePudding user response:

We could group and then use summarise with across:

library(dplyr)

df %>% 
  group_by(Year) %>% 
  summarise(across(-Country, sum))
   Year  exports  imports
  <int>    <int>    <int>
1  2004 20000000 20000000
2  2008 40000000 40000000
3  2009 40000000 60000000

CodePudding user response:

If you use the sum() function and your data.frame has missing values (e.g. NA), you have to pass the argument na.rm = TRUE in the sum() function call. Then your code above will work.

  • Related