[R] tapply question

From: <markleeds_at_verizon.net>
Date: Thu 06 Jul 2006 - 23:59:18 EST


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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jul 07 00:04:57 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 07 Jul 2006 - 02:14:37 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.