我一直有一个问题,似乎相对简单,但我没有能够正确地做到这一点。我看到它是通过按一列分组完成的,但我不确定如何解决这个问题,因为我试图附加不同值的月份。
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.
我们可以使用dplyr
,
library(tidyverse)
data %>%
group_by(country, year) %>%
summarise(
total = sum(Total)
)
我们可以使用base R
(不需要包)
aggregate(Total ~ ., df1[c('Country', 'Year', 'Total')], sum)