[R] Quickest way to access data.frame?

From: yoooooo <magno_yu_at_ml.com>
Date: Wed, 02 Apr 2008 07:57:32 -0700 (PDT)

Hi, I have tried search around this forum for the best way to access a data.frame.. i got the feeling that "no partial match" is the way to make it fast.... so I convert everything to factor.. but I'm still not 100% sure if the following code will do it... is this the fastest way to do something foreach (ID, ID2) pair? Thanks!

data <- data.frame(ID = floor(runif(n = 40000, min=0, max=20)),

           ID2 = floor(runif(n = 40000, min=0, max=1000)), 
           DATA1 = rnorm(n = 40000))

data$ID <- as.factor(data$ID)
data$ID2 <- as.factor(data$ID2)
res <- lapply(attr(data$ID, "levels"), function(x, data){   data <- data[which(data$ID == x), ]
  res <- structure(unlist(lapply(attr(data$ID2, "levels"), function(x, data){
     data <- data[which(data$ID2 == x), ]
     if (nrow(data) == 0){
        return(NA)
     }
     data[nrow(data), "DATA1"]

  }, data = data)), names=as.character(attr(data$ID2, "levels")))   res <- mean(res, na.rm=TRUE)
}, data = data)
-- 
View this message in context: http://www.nabble.com/Quickest-way-to-access-data.frame--tp16447176p16447176.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org 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 Wed 02 Apr 2008 - 17:24:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 02 Apr 2008 - 19:30:25 GMT.

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

list of date sections of archive