[R] Sum up column values according to row id

arun smartpink111 at yahoo.com
Wed Apr 24 20:31:53 CEST 2013


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



>Dear All, 
>
>here a problem I think many of you can solve in few minutes. 
>
>I have a dataframe which contains values of plot id, diameters, heigths and 
>basal area of trees, thus columns names are: id | dbh | h | g 
>
>head(ipso, n=10)        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 
>
>from here I need to calculate the mean of the six greater g_ith for 
>each id_ith. The clauses are that: 
>
>if length(id) >=6 
>
>do the mean of the first six greaters g 
>
>
>else 
>do the mean of all the g_ith in the id_ith (in head print above e.g. 
>for the id==FPE0164 do the mean of just these two values of g). 
>
>The g are already ordered by id ascending and g descending using: 
>
>ipso <- ipso[with(ipso, order(ipso$id, -ipso$g)), ] # Order for id 
>ascending and g descending 
>
>I tried a lot of for loops and tapply() without results. 
>
>Can anyone help me to solve this? 
>
>Thanks for your attention 
>
>Best whishes 
>
>Matteo 


______________________________________________ 
[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.



More information about the R-help mailing list