Message-ID: <434A8CFF.5070300@fz-rossendorf.de>
Date: 2005-10-10T15:47:11Z
From: Joerg van den Hoff
Subject: 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