How to find of sum of values within a dataframe in R? -


this question has answer here:

i have data frame:

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) 

how create new date frame lists letter column x , gives sum of values column y? want create:

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

this easy dplyr,

if don't have

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)) 

Comments