[R] Subset with selection variable from function argument. Is there another way?

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed May 11 11:13:55 CEST 2005

Fredrik Karlsson wrote:

> Dear list,
> I'm making my current code more generic and would like some advise.
> The basic problem is subset and the name of the column to be compared
> for selection.
> What I've come up with is 
>>set <- bottompremolars"

The line above is
  a) syntactically incorrect and
  b) the string does not describe a variable in the mammals data
hence this is not reproducible at all.

>>subset(mammals, eval(parse(file="",text=set)) > 2)

Let's assume
   set <- "body"

Either use get() as in
   subset(mammals, get(set) > 2)
or simple indexing such as:
   subset(mammals, mammals[[set]] > 2)

Uwe Ligges

> This seems a bit odd.  Is there a nicer way?
> /Fredrik
> ______________________________________________
> 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

More information about the R-help mailing list