-1

I have a dataframe which looks like this -

>df
words num 
his    92   
his    91   
there  91   
you    90   
who    90   
come   89   
you    70   

Now, I want to aggregate the common words and sum their num column. So my final output would be like -

words   num
his     183   
there   91   
you     160   
who     90   
come    89   

How can I do this in R? I know it is something simple but not able to figure it out.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

2

Convert to a data.table:

setDT(df)
df[, sum(num), by=words]
   words  V1
1:   his 183
2: there  91
3:   you 160
4:   who  90
5:  come  89
Chris Watson
  • 1,347
  • 1
  • 9
  • 24