Access variables by string
Philipp Rappold wrote:
Dear all,
I have two probably very easy questions:
(1) Is there a way to access certain variables by their string-based
name representation?
Example:
numbers <- c("one", "two", "three")
varname <- "numbers"
print(varname[2])
(2) I need this functionality for a customized na.exclude() function
that I am building, which should only exclude rows that have NA in
certain columns. Maybe there is already a function which does exactly
what I need, so I'd highly appreciate if someone could point me there ;)
My current implementation looks like this:
naexlcude <- function(data, varnames)
{
for(v in varnames){
data = subset(data, !is.na(v))
}
data
}
Well, you can use get(varname)[2] or more generally things like eval(bquote(.(as.name(varname))[2])), but for this particular application, why not just use the standard indexing techniques? I think this will do ix <- apply(is.na(data[varnames]), 1, any) data[!ix,] e.g.
table(apply(is.na(airquality[c("Wind","Ozone")]),1, any))
FALSE TRUE 116 37
colSums(is.na(airquality))
Ozone Solar.R Wind Temp Month Day
37 7 0 0 0 0
table(apply(is.na(airquality[c("Solar.R","Ozone")]),1, any))
FALSE TRUE 111 42 (Using functions like subset inside another function often leads to problems because of the nonstandard evaluation tricks that it uses. It is mainly useful to save tying on the command line.)
O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907