[R] mean of subset of rows

darteta001 at ikasle.ehu.es darteta001 at ikasle.ehu.es
Mon Oct 1 17:57:40 CEST 2007

Dear list, 
this must be an easy one:

I have a data.frame of two columns, "ID" with four different levels (A 
to D) and numerical "size", and each of the 4 different IDs is 
repeated a 
different number of times. I would like to get the mean size for each 
ID as another data.frame. I have tried the following:

>ID= as.character(unique(data[,1])) # I use unique() because "data" 
will be larger in future
>nIDs = length(ID)
>for(i in 1:nIDs){
+  subdata = subset(data,V1==ID[i])
+  average = as.data.frame(cbind(1:i,ID[i],mean(subdata[,2]))
+ }

Unfortunately, my output only gets the last level of ID four times:
     V1 V2               V3
1  1  D 179.777777777778
2  2  D 179.777777777778
3  3  D 179.777777777778
4  4  D 179.777777777778

How can I get what I need? there might be an easier way to do it, but 
I guess my skills aren´t that good. Any suggestions are welcome



More information about the R-help mailing list