Skip to content

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

3 messages · Fredrik Karlsson, Uwe Ligges, Brian Ripley

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

/Fredrik
#
Fredrik Karlsson wrote:

            
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.
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
#
On Wed, 11 May 2005, Fredrik Karlsson wrote:

            
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.