Thank you very much Doct. Carlson!!! The function you suggest me wors perfectely!!!
Thanks a lot again, Best whishes sincerely Mt M 2013/4/24 David Carlson <dcarl...@tamu.edu> > Something like this? > > mean6 <- function(x) { > if (length(x) < 6) { > mn <- mean(x) > } else { > mn <- mean(x[1:6]) > } > return(mn) > } > > aggregate(g~id, ipso, mean6) > > ------------------------------------- > David L Carlson > Associate Professor of Anthropology > Texas A&M University > College Station, TX 77840-4352 > > > > > > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > On > Behalf Of Matteo Mura > Sent: Wednesday, April 24, 2013 7:57 AM > To: r-help@r-project.org > Subject: [R] Sum up column values according to row id > > 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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.