Re: [R] Quickest way to access data.frame?

From: Erik Iverson <iverson_at_biostat.wisc.edu>
Date: Wed, 02 Apr 2008 13:33:36 -0500

Hello -

I am having a very hard time deciding what it is you want here. Could you be more specific? Given your 'data' data.frame, what do you want the output to be?

yoooooo wrote:
> 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!

I doubt it. Have a look at ?tapply, ?by, ?aggregate, and ?outer for possible approaches to what you might be trying to do.

Best,
Erik Iverson

>
> 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)
>
>
>



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 - 19:02:47 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 - 20:31:07 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