# [R] Sum up column values according to row id

Wed Apr 24 20:43:55 CEST 2013

```Hello,

Note also that in the with(9 instruction there's some redundancy, th OP
could simply do

with(ipso, order(id, -g))

Hope this helps,

Em 24-04-2013 19:10, David Carlson escreveu:
> 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-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Matteo Mura
> Sent: Wednesday, April 24, 2013 7:57 AM
> To: r-help at 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?
>
>
> Best whishes
>
> Matteo
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help