如何通过子集函数的函数传递表达式以在 R 中求值 [英] how to pass an expression through a function for the subset function to evaluate in R
问题描述
我正在尝试为不同的对象类编写一个子集方法,我希望用户能够以与使用 subset.data.frame
函数相同的方式执行该方法.我已经阅读了一些相关文章,例如 this 和 this,但我不认为它们是这里的解决方案.我相信我使用了错误的环境,但我对环境和 substitute
函数的了解不够,无法弄清楚为什么这段代码的前半部分有效,而后半部分却没有.谁能解释我在下面做错了什么,然后如何制作一个可以将 gear == 4
作为第二个参数的有效 lsubset
函数?抱歉,如果我的搜索遗漏了类似的问题..谢谢!!
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 )
推荐答案
关于:
lsubset <-
function( x , ... ){
lapply( x , subset , ... )
}
lsubset( mtlist , gear == 4 )
这篇关于如何通过子集函数的函数传递表达式以在 R 中求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!