[R] question about sum of (column) elements in R

arun smartpink111 at yahoo.com
Tue Dec 4 13:41:04 CET 2012


HI,
You can use either ?tapply(), ?aggregate(), ?ddply() from library(plyr)
dat1<-read.table(text="
0  12
1  10
1  4
1  6
1  7
1  13
2  21
2  23
2  20
3  18
3  17
3  16
3  27
3  33
4  11
4  8
4  19
4  16
4  9
",sep="",header=FALSE)
 with(dat1,aggregate(dat1[,2],by=list(V1=dat1[,1]),sum))
 # V1   x
#1  0  12
#2  1  40
#3  2  64
#4  3 111
#5  4  63
A.K.




----- Original Message -----
From: T Bal <studenttbal at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, December 4, 2012 2:59 AM
Subject: [R] question about sum of (column) elements in R

Hi,
I have the following data:

0   12
1   10
1   4
1   6
1   7
1   13
2   21
2   23
2   20
3   18
3   17
3   16
3   27
3   33
4   11
4   8
4   19
4   16
4   9


In this data file I would like to sum the numbers of second column which
belong to the same number in the first column.
So the output would be:

0     12
1     40
2     64
        3     111
        etc.

Thank you.

Kind regards,
T. Bal

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list