Re: [R] tapply question

From: Jacques VESLOT <jacques.veslot_at_good.ibl.fr>
Date: Fri 07 Jul 2006 - 00:10:33 EST

i think you can't have column with the same names.

 > data.frame(AAA=1:3, AAA=4:6)
   AAA AAA.1

1   1     4
2   2     5
3   3     6

but you could subset the data frame by names using substring():

sapply(unique(substring(names(data1), 1, 3)), function(x)

        rowMeans(data1[, substring(names(data1), 1, 3) == x])



Jacques VESLOT

CNRS UMR 8090
I.B.L (2ème étage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex

Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31

http://www-good.ibl.fr


markleeds@verizon.net a écrit :
> 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
>



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:16:13 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.