Re: [R] Getting the groupmean for each person

About this list Date view Thread view Subject view Author view Attachment view

From: Christophe Pallier (pallier@lscp.ehess.fr)
Date: Mon 10 May 2004 - 18:09:17 EST


Message-id: <409F38AD.80305@lscp.ehess.fr>


Liaw, Andy wrote:

>Suppose I
>define the function:
>
>fun <- function(x, f) {
> m <- tapply(x, f, mean)
> ans <- x - m[match(f, unique(f))]
> names(ans) <- names(x)
> ans
>}
>
>
>

May I ask what is the purpose of match(f,unique(f)) ?

To remove the group means, I have be using:

x-tapply(x,f,mean)[f]

for a while, (and I am now changing to
x-tapply(x,f,mean)[as.character(f)] because of the peculiarities of
indexing named vectors with factors )

The use of tapply(x,f,mean)[match(f,unique(f))] assumes a particular
order in the result of tapply, no? It seems a bit dangerous to me.

Christophe Pallier

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Mon 31 May 2004 - 23:05:08 EST