Re: [R] "fixed effects" transformation

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Fri 25 Aug 2006 - 01:04:38 EST

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri Aug 25 01:07:40 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 25 Aug 2006 - 02:23:23 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.