-1

I've been having an issue that seems relatively simple, but I haven't been able to do it correctly. I saw that it was done with just grouping by one column, but I'm not sure how to tackle this since I'm trying to append months that are different values.

Country Year Month Total
Brazil  2007   1     20
Brazil  2007   2     20
Brazil  2007   3     20
Canada  2007   1     10
... 
Brazil  2008   10   10


I'm trying to add the monthly total(1-12) for each country per each year(ex:2007-2010 so rather than the month tab I just have the yearly total for one country.





2 Answers2

1

We can use base R (no packages needed)

aggregate(Total ~ ., df1[c('Country', 'Year', 'Total')], sum)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

We can use dplyr for this,

library(tidyverse)

data %>%
    group_by(country, year) %>%
    summarise(
          total = sum(Total)
)
Serkan
  • 1,855
  • 6
  • 20