1

i'm trying to write a subset method for a different object class that i'd like users to be able to execute the same way they use the subset.data.frame function. i've read a few related articles like this and this, but i don't think they're the solution here. i believe i'm using the wrong environment, but i don't understand enough about environments and also the substitute function to figure out why the first half of this code works but the second half doesn't. could anyone explain what i'm doing wrong below and then how to make a working lsubset function that could take gear == 4 as its second parameter? sorry if my searches missed a similar question.. thanks!!

# create a list of mtcars data frames
mtlist <- list( mtcars , mtcars , mtcars )
# subset them all - this works
lapply( mtlist , subset , gear == 4 )


# create a function that simply replicates the task above
lsubset <- 
    function( x , sset ){
        lapply( x , subset , sset )
    }

# this does not work for some reason
lsubset( mtlist , gear == 4 )
Community
  • 1
  • 1
Anthony Damico
  • 5,779
  • 7
  • 46
  • 77
  • 1
    I'd take a look at [this](http://stackoverflow.com/questions/9860090/in-r-why-is-better-than-subset) question thread and [this one](http://stackoverflow.com/questions/12850141/programming-safe-version-of-subset-to-evaluate-its-condition-while-called-from/12850252#12850252). It is commonly said that using `subset` programatically is a bad idea, and I think you're about to discover why! – Justin Jul 01 '13 at 15:17

1 Answers1

5

What about:

lsubset <- 
    function( x , ... ){
        lapply( x , subset , ... )
    }

lsubset( mtlist , gear == 4 )
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519