R中%in%运算符的重载

b

我在重载R中的%in%运算符时遇到问题。在重载==运算符之后,也因为我的“ Type”类不是R和%in%中的基本值:

setMethod("==", signature(e1 = "Type", e2 = "ANY"), function (e1, e2) {
    class(e2)=="Type" && e1$name == e2$name
})  
setMethod("==", signature(e1 = "ANY", e2 = "Type"), function (e1, e2) {
    class(e1)=="Type" && e1$name == e2$name
})  

setMethod("%in%", signature(e1 = "Type", e2 = "list"), function (e1, e2) {
    for (i in e2) {
        if (e1 == i)
            return(TRUE);
    }
    return(FALSE);
})

最后一个方法返回以下错误

Creating a generic function from function ‘%in%’ in the global environment
Errore in match.call(definition, call, expand.dots) : 
    unused arguments (e1 = c("Type", ""), e2 = c("list", ""))

我该如何解决我的问题?提前致谢

布罗迪

你有错误的论点。看着:

`%in%`
function (x, table) 
match(x, table, nomatch = 0L) > 0L
<bytecode: 0x7ffd5984e978>
<environment: namespace:base>

您需要将方法更改为具有xtable作为参数。这就是为什么会出现错误的原因unused arguments (e1 = c("Type", ""), e2 = c("list", "")),因为e1e2不是通用定义的一部分。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章