how to pass an expression through a function for the subset function to evaluate in R

我与影子孤独终老i 提交于 2019-12-31 04:48:07

问题


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 )

回答1:


What about:

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

lsubset( mtlist , gear == 4 )


来源:https://stackoverflow.com/questions/17407852/how-to-pass-an-expression-through-a-function-for-the-subset-function-to-evaluate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!