I found myself wanting to average a vector [vec] within each level of a factor [Fac], returning a vector of the same length as vec. After a while I realised that lm1 <- lm(vec ~ Fac) fitted(lm1) did what I want. But there must be another way to do this, and it would be good to be able to apply other functions than mean() in this way. Cheers, Murray -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: maj at waikato.ac.nz Fax 7 838 4155 Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862
G'day Murray,>>>>> "MJ" == Murray Jorgensen <maj at stats.waikato.ac.nz> writes:MJ> I found myself wanting to average a vector [vec] within each MJ> level of a factor [Fac], returning a vector of the same length MJ> as vec. I presume that the vector that you want as result should not just have the same length as vec, should it? :-) MJ> But there must be another way to do this, and it would be good MJ> to be able to apply other functions than mean() in this way. Something like:> Fac <- sample(gl(2,4)) > Fac[1] 2 1 1 2 2 1 1 2 Levels: 1 2> vec <- rnorm(length(Fac)) > tapply(vec, Fac, mean)1 2 -0.6435816 -0.9267021> tapply(vec, Fac, mean)[Fac]2 1 1 2 2 1 1 -0.9267021 -0.6435816 -0.6435816 -0.9267021 -0.9267021 -0.6435816 -0.6435816 2 -0.9267021> tapply(vec, Fac, sum)[Fac]2 1 1 2 2 1 1 2 -3.706808 -2.574326 -2.574326 -3.706808 -3.706808 -2.574326 -2.574326 -3.706808 Cheers, Berwin ========================== Full address ===========================Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr) School of Mathematics and Statistics +61 (8) 6488 3383 (self) The University of Western Australia FAX : +61 (8) 6488 1028 35 Stirling Highway Crawley WA 6009 e-mail: berwin at maths.uwa.edu.au Australia http://www.maths.uwa.edu.au/~berwin
Hi vec<-runif(100) fac<-factor(sample(letters[1:4],100, rep=T)) tap<-tapply(vec, fac, mean) new.vec<-tap[fac] lm1 <- lm(vec ~ fac) all.equal(as.numeric(predict(lm1)),as.numeric(new.vec)) [1] TRUE HTH Petr On 14 Apr 2006 at 17:46, Murray Jorgensen wrote: Date sent: Fri, 14 Apr 2006 17:46:12 +1200 From: Murray Jorgensen <maj at stats.waikato.ac.nz> To: r-help at stat.math.ethz.ch Subject: [R] vector-factor operation> I found myself wanting to average a vector [vec] within each level of > a factor [Fac], returning a vector of the same length as vec. After a > while I realised that > > lm1 <- lm(vec ~ Fac) > fitted(lm1) > > did what I want. > > But there must be another way to do this, and it would be good to be > able to apply other functions than mean() in this way. > > Cheers, Murray > -- > Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html > Department of Statistics, University of Waikato, Hamilton, New Zealand > Email: maj at waikato.ac.nz Fax 7 838 4155 > Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862 > > ______________________________________________ > R-help at 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.htmlPetr Pikal petr.pikal at precheza.cz
Look at ?ave ave(vec, Fac) ave(vec, Fac, FUN = mean) # same ave(vec, Fac, FUN = sd) On 4/14/06, Murray Jorgensen <maj at stats.waikato.ac.nz> wrote:> I found myself wanting to average a vector [vec] within each level of a > factor [Fac], returning a vector of the same length as vec. After a > while I realised that > > lm1 <- lm(vec ~ Fac) > fitted(lm1) > > did what I want. > > But there must be another way to do this, and it would be good to be > able to apply other functions than mean() in this way. > > Cheers, Murray > -- > Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html > Department of Statistics, University of Waikato, Hamilton, New Zealand > Email: maj at waikato.ac.nz Fax 7 838 4155 > Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862 > > ______________________________________________ > R-help at 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 >
On Fri, 14 Apr 2006, Murray Jorgensen wrote:> I found myself wanting to average a vector [vec] within each level of a > factor [Fac], returning a vector of the same length as vec. After a > while I realised that > > lm1 <- lm(vec ~ Fac) > fitted(lm1) > > did what I want. > > But there must be another way to do this, and it would be good to be > able to apply other functions than mean() in this way. >ave(vec,Fac) and, yes, it can take a different function as an argument. -thomas
Thanks to Berwin Turlach and Petr Pikal for tapply(vec, Fac, mean)[Fac] and Gabor Grothendieck Thomas Lumley for ave(vec,Fac) . Looking at the code for tapply and ave I guess that the latter is to be preferred. Murray Jorgensen Gabor Grothendieck wrote:> Look at ?ave > > ave(vec, Fac) > ave(vec, Fac, FUN = mean) # same > ave(vec, Fac, FUN = sd) > > On 4/14/06, Murray Jorgensen <maj at stats.waikato.ac.nz> wrote: >> I found myself wanting to average a vector [vec] within each level of a >> factor [Fac], returning a vector of the same length as vec. After a >> while I realised that >> >> lm1 <- lm(vec ~ Fac) >> fitted(lm1) >> >> did what I want. >> >> But there must be another way to do this, and it would be good to be >> able to apply other functions than mean() in this way. >> >> Cheers, Murray-- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: maj at waikato.ac.nz Fax 7 838 4155 Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862