-1

I have a data set that looks like this. You can see that column X2 has key values. I would like to sum the pair values from column X1 by the key values in column X2. I have been trying to use ddply or aggregate but I am running into problems. Does anyone have a simple way to do this?

> X1=rnorm(30,mean=20, sd=10)
> X2=rep(1:15,2)
>df= data.frame(X1,X2)
          X1 X2
1  10.065545  1
2   6.938315  2
3   5.657782  3
4  11.371521  4
5  37.037992  5
6  13.443674  6
7   8.868314  7
8   5.944946  8
9  18.493563  9
10 16.059931 10
11 22.520693 11
12 17.172936 12
13 28.676676 13
14 27.200911 14
15 30.560696 15
16 22.435021  1
17 31.143627  2
18 19.234473  3
19 29.329251  4
20 18.420183  5
21 13.184905  6
22  7.798989  7
23 12.910870  8
24 12.513204  9
25 25.760775 10
26 19.479005 11
27 13.696628 12
28 11.016639 13
29 35.126859 14
30 17.632507 15
Max
  • 837
  • 4
  • 11
  • 20
  • What is the code you tried? What are the "problems" you ran into. This seems like the most basic of aggregating problems. Also, in this format it's difficult to tell the classes of the columns in your data frame. It's better to share a `dput()` of the objects involved. See [how to create a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Sep 20 '14 at 05:43
  • This is exactly what I wanted, thanks!!! Sorry if I was vague – Max Sep 20 '14 at 05:53

3 Answers3

5

I can't reproduce your exact data due to your use of rnorm, but this is a simple aggregation. The formula method is best for this type.

aggregate(X1 ~ X2, df, sum)
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
1

For bigger datasets, you could use the data.table option

 library(data.table)
 setDT(df)[, list(sum(X1, na.rm=TRUE)), by=X2]

Or dplyr

 library(dplyr)
 df %>% 
     group_by(X2) %>%
     summarise(X1=sum(X1, na.rm=TRUE))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

tapply can also be used:

with(df, tapply(X1, X2, sum))
       1        2        3        4        5        6        7        8        9       10       11       12       13       14 
26.99725 24.88007 16.62240 27.98272 62.02971 38.87095 19.60940 41.50565 61.22318 48.31883 17.12463 51.68966 53.16138 11.21039 
      15 
43.55936 
rnso
  • 23,686
  • 25
  • 112
  • 234