Skip to content
Prev 322324 / 398500 Next

Sum up column values according to row id

HI
Not sure whether this helps:
ipso<- read.table(text="
?? id dbh???? h????????? g
1? FPE0164? 36 13.62 0.10178760
2? FPE0164? 31 12.70 0.07547676
21 FPE1127? 57 18.85 0.25517586
13 FPE1127? 39 15.54 0.11945906
12 FPE1127? 34 14.78 0.09079203
6? FPE1127? 32 15.12 0.08042477
5? FPE1127? 28 14.13 0.06157522
15 FPE1127? 27 13.50 0.05725553
19 FPE1127? 25 13.28 0.04908739
11 FPE1127? 19 11.54 0.02835287 
",sep="",header=TRUE,stringsAsFactors=FALSE)

library(plyr)
ddply(ipso,.(id),summarize,g=mean(head(g,6)))
?# ???? id????????? g
#1 FPE0164 0.08863218
#2 FPE1127 0.11078041


aggregate(g~id,ipso,function(x) mean(head(x,6)))
#?????? id????????? g
#1 FPE0164 0.08863218
#2 FPE1127 0.11078041
______________________________________________ 
[hidden email] 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.