[R] how to calculate weighted means by groups
Patrick Zimmermann
brassnotdead at googlemail.com
Thu Aug 30 13:01:55 CEST 2007
Hello,
I want to calculate the weighted means of a table column, aggregated
by other elements of the same table.
For example I want to calculate mean species numbers weighted by area
and grouped by year, altitude and/or region.
I think a combination of aggregate() and weighted.mean() should work,
but I always get 'Errors'.
this was my favorite:
aggregate(tab, list(year = tab[,"year"], altitude= tab[,"altit"],
region= tab[,"region"]),
weighted.mean(tab[,"spcs"], tab[,"area"]))
with a table like:
spcs type altit area year region
38 a low 202 1980 W
40 a low 326 2000 W
45 b medium 207 1980 W
48 b medium 205 2000 W
50 a low 45 1980 E
53 a low 104 2000 E
43 c medium 7 1980 E
33 c medium 9 2000 E
23 c high 20 1980 E
13 c high 26 2000 E
Who can help?
Thanks in advance,
Patrick
More information about the R-help
mailing list