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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 11 May 2005 - 20:22:15 EST

On Wed, 11 May 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
>
>> data(mammals)
>> set <- bottompremolars"
>> subset(mammals, eval(parse(file="",text=set)) > 2)
>
> This seems a bit odd. Is there a nicer way?

Try

set <- "bottompremolars"
mammals[[set]]

assuming it is a data frame and you just want the one column.

subset() is just a convenience wrapper for the basic indexing operations, which are well worth learning.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@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
Received on Wed May 11 20:28:48 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:41 EST