[R] how to assign a group mean to individual cases?
Emmanuel Charpentier
charpent at bacbuc.dyndns.org
Tue Nov 13 15:09:27 CET 2007
Casey Klofstad a écrit :
> I need advice on how to create a variable that is the group mean of
> another variable.
>
> For example, I have a variable called x for which each row in the data
> set has a value. I also have a nominal variable called g that
> indicates which of 100 different groups each row belongs to.
>
> So, I want to create a new variable called w, which is the group mean
> of x for which ever group the row belongs to (as indicated by variable
> g). Ideally, I'd also like to take out each row's value of x before
> calculating the group mean assigned to that row.
>
> I've already tried the aggregate command. That gives me the group
> means, but does not allow me to assign them to each row in the data
> set.
The first one is easy : you just have to choose which siege cannon you
need to shoot your fly. What about :
your.dataset$w<-(aov(x~g,data=your.dataset))$fitted.values
No a priori idea about the second one, but it has a strong flavor of
jackknife ; I'd have a look in this direction...
HTH,
Emmanuel Charpentier
More information about the R-help
mailing list