joerg van den hoff
2005-Oct-10 15:47 UTC
[R] problem with lapply(x, subset, ...) and variable select argument
I need to extract identically named columns from several data frames in a list. the column name is a variable (i.e. not known in advance). the whole thing occurs within a function body. I'd like to use lapply with a variable 'select' argument. example: tt <- function (n) { x <- list(data.frame(a=1,b=2), data.frame(a=3,b=4)) for (xx in x) print(subset(xx, select = n)) ### works print (lapply(x, subset, select = a)) ### works print (lapply(x, subset, select = "a")) ### works print (lapply(x, subset, select = n)) ### does not work as intended } n = "b" tt("a") #works (but selects not the intended column) rm(n) tt("a") #no longer works in the lapply call including variable 'n' question: how can I enforce evaluation of the variable n such that the lapply call works? I suspect it has something to do with eval and specifying the correct evaluation frame, but how? .... many thanks joerg
Thomas Lumley
2005-Oct-10 17:22 UTC
[R] problem with lapply(x, subset, ...) and variable select argument
On Mon, 10 Oct 2005, joerg van den hoff wrote:> I need to extract identically named columns from several data frames in > a list. the column name is a variable (i.e. not known in advance). the > whole thing occurs within a function body. I'd like to use lapply with a > variable 'select' argument.You would probably be better off using "[" rather than subset(). tt <- function (n) { x <- list(data.frame(a=1,b=2), data.frame(a=3,b=4)) print(lapply(x,"[",n)) } seems to do what you want. -thomas> example: > > tt <- function (n) { > x <- list(data.frame(a=1,b=2), data.frame(a=3,b=4)) > for (xx in x) print(subset(xx, select = n)) ### works > print (lapply(x, subset, select = a)) ### works > print (lapply(x, subset, select = "a")) ### works > print (lapply(x, subset, select = n)) ### does not work as intended > } > n = "b" > tt("a") #works (but selects not the intended column) > rm(n) > tt("a") #no longer works in the lapply call including variable 'n' > > > question: how can I enforce evaluation of the variable n such that > the lapply call works? I suspect it has something to do with eval and > specifying the correct evaluation frame, but how? .... > > > many thanks > > joerg > > ______________________________________________ > R-help at 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 >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
Gabor Grothendieck
2005-Oct-10 17:27 UTC
[R] problem with lapply(x, subset, ...) and variable select argument
The problem is that subset looks into its parent frame but in this case the parent frame is not the environment in tt but the environment in lapply since tt does not call subset directly but rather lapply does. Try this which is similar except we have added the line beginning with environment before the print statement. tt <- function (n) { x <- list(data.frame(a=1,b=2), data.frame(a=3,b=4)) environment(lapply) <- environment() print(lapply(x, subset, select = n)) } n <- "b" tt("a") What this does is create a new version of lapply whose parent is the environment in tt. On 10/10/05, joerg van den hoff <j.van_den_hoff at fz-rossendorf.de> wrote:> I need to extract identically named columns from several data frames in > a list. the column name is a variable (i.e. not known in advance). the > whole thing occurs within a function body. I'd like to use lapply with a > variable 'select' argument. > > > example: > > tt <- function (n) { > x <- list(data.frame(a=1,b=2), data.frame(a=3,b=4)) > for (xx in x) print(subset(xx, select = n)) ### works > print (lapply(x, subset, select = a)) ### works > print (lapply(x, subset, select = "a")) ### works > print (lapply(x, subset, select = n)) ### does not work as intended > } > n = "b" > tt("a") #works (but selects not the intended column) > rm(n) > tt("a") #no longer works in the lapply call including variable 'n' > > > question: how can I enforce evaluation of the variable n such that > the lapply call works? I suspect it has something to do with eval and > specifying the correct evaluation frame, but how? .... > > > many thanks > > joerg > > ______________________________________________ > R-help at 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 >