Aggregate rows by shared values in a variable

末鹿安然 提交于 2019-12-03 20:49:06

aggregate(x = df$Match, by = list(df$Year), FUN = sum), assuming df is your data frame above.

Jilber Urbina

You may also want to use 'ddply' function from 'plyr' package.

# install plyr package
install.packages('plyr')
library(plyr)
# creating your data.frame
foo <- as.data.frame(structure(c(2008L, 2008L, 2008L, 2008L, 2007L, 2007L, 2007L, 
            2006L, 2006L, 2006L, 1808L, 137088L, 1L, 56846L, 2704L, 169876L, 
            75750L, 2639L, 193990L, 2L), .Dim = c(10L, 2L), .Dimnames = list(
              NULL, c("Year", "Match"))))

# here's what you're looking for
ddply(foo,.(Year),numcolwise(sum))

  Year  Match
1 2006 196631
2 2007 248330
3 2008 195743

By the way, the total sum for 2008 should be 195743 (1808+137088+1+56846) instead of 138897 you forgot add 56846 up.

As it is explained above, you can use aggregate to do it as follows. but in a much simpler way

aggregate(. ~ Year, df, sum)
#  Year  Match
#1 2006 196631
#2 2007 248330
#3 2008 195743

You can also use the Dplyr to solve this as follows

library(dplyr)
df %>% group_by(Year) %>% summarise(Match = sum(Match))
#  Year  Match
#  (int)  (int)
#1  2008 195743
#2  2007 248330
#3  2006 196631
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!