[R] vector-factor operation

Thomas Lumley tlumley at u.washington.edu
Fri Apr 14 18:09:00 CEST 2006


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




More information about the R-help mailing list