[R] "fixed effects" transformation

Thomas Lumley tlumley at u.washington.edu
Thu Aug 24 17:04:38 CEST 2006


On Wed, 23 Aug 2006, Eduardo Leoni wrote:
>
> I created this function following Farnsworth
> (http://cran.r-project.org/doc/contrib/Farnsworth-EconometricsInR.pdf)
>
>
> demean <- function(x,index) {
>  for (i in unique(index)) {
>    for (j in 1:ncol(x)) {
>      x.now <- x[index==i,j]
>      x[index==i,j] <- x.now-mean(x.now,na.rm=TRUE)
>    }
>  }
>  x
> }
>
> it is obvious that there must be a much much more efficient way to do
> this, though. Any recommendations?

I think  you want ave().

 	-thomas



More information about the R-help mailing list