[R] vector-factor operation
Murray Jorgensen
maj at stats.waikato.ac.nz
Sat Apr 15 00:16:11 CEST 2006
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
More information about the R-help
mailing list