[R] Access variables by string

Benilton Carvalho beniltoncarvalho at gmail.com
Thu Feb 11 17:44:23 CET 2010


On Thu, Feb 11, 2010 at 4:18 PM, Philipp Rappold
<philipp.rappold at gmail.com> 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])

print(get(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
> }

f = function(x, vars) x[complete.cases(x[vars]),]

b

>
> Best
> Philipp
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list