-1

I have the 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 do I create a new date frame that lists the letter from column x and then gives the sum of the values from column y? This is what I want to create:

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

1 Answers1

1

This easy with dplyr,

If you don't have it

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))
lbollar
  • 1,005
  • 1
  • 10
  • 17