[R] tapply question

markleeds at verizon.net markleeds at verizon.net
Thu Jul 6 15:59:18 CEST 2006


I think I understand tapply but i still
can't figure out how to do the following.

I have a dataframe where some of the column names are the same
and i want to make a new dataframe where columns
that have the same name are averaged by row.

so, if the data frame, DF, was 

AAA    BBB      CCC   AAA DDD
1       0        7     11  13
2        0       8     12  14
3        0       6      0  15

then the resulting data frame would be exactly the same except
that the AAA column would be 

6   comes from  (11 + 1)/2
7    comes from  (12 + 2)/2
3   stays 3 because the element in the other AAA is zero
so i don't want to average that one. it shoulsd just stay 3.

So, I do 

DF[DF == 0]<-NA
rowaverage<-function(x) x[rowMeans(forecastDf[x],na.rm=TRUE)
revisedDF<-tapply(seq(DF),names(DF),rowmeans)

there are two problems with this :

1) i need to go through the rows of the same name, not the columns
so i don't think seq(DF) is right because that goes through 
the columns but i want to go through rows.

2) BBB will come back with ALL NA's ( since
it was unique and there was nothing else to average ( and I don't know how to transform that BB column to all zero's.

thanks and i'm sorry for so many questions. i'm getting bettter with this stuff and my questions will decrease soon.

my guess is that i no longer should be using tapply ?
and should be using some other version of apply.
thanks
                                         mark



More information about the R-help mailing list