2015-07-09 48 views
-1

我有數據幀:如何查找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

這容易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))