0

Consider the following data:

df <- data.frame(sex     = c("MALE","FEMALE","MALE","FEMALE"),
                 time    = c("2018","2018","2019","2019"),
                 country = c("USA","USA","USA","USA"),
                 value   = c(10,10,10,10))

How do I aggregate the "sex" axis, so I get the following data:

    TIME    Country   Value
1   2018    USA       20
2   2019    USA       20

How do I do that in R (I know that it is probably a simple question)?

Michael
  • 565
  • 4
  • 11

1 Answers1

1

Using dplyr, the following does what you requested:

df %>% group_by(time, country)%>% summarize(sumval = sum(value))
# A tibble: 2 x 3
# Groups:   time [2]
  time  country sumval
  <chr> <chr>    <dbl>
1 2018  USA         20
2 2019  USA         20
gaut
  • 5,771
  • 1
  • 14
  • 45