[R] Access variables by string

Don MacQueen macq at llnl.gov
Thu Feb 11 18:00:43 CET 2010

For your first question, use the get() function


At 5:18 PM +0100 2/11/10, 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?
>numbers <- c("one", "two", "three")
>varname <- "numbers"
>(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
>R-help at r-project.org mailing list
>PLEASE do read the posting guide http://*www.*R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

More information about the R-help mailing list