我有数据框:
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
答案 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))