如何在R中的数据帧中找到值的总和?

时间:2015-07-09 22:43:07

标签: r

我有数据框:

x=c("a","a","b","b","b","c","c","d","e","f","f")
y=c(2,3,6,2,5,6,2,1,4,3,2)
df=data.frame(x,y)

如何创建一个新的日期框架,列出x列中的字母,然后给出y列中值的总和?这就是我想要创建的内容:

   x y
1  a 5
2  b 13
3  c 11
4  d 2
5  e 4
6  f 5   

1 个答案:

答案 0 :(得分:1)

这很容易用dplyr,

如果你没有它

install.packages("dplyr")

library(dplyr)

x=c("a","a","b","b","b","c","c","d","e","f","f")
y=c(2,3,6,2,5,6,2,1,4,3,2)
df=data.frame(x,y)

df <-df %>% group_by(x) %>% summarise(y = sum(y))