我有一个 S4 类,我想定义这些对象的线性组合。
是否可以派送*
和 +
这个特定类的函数?
最佳答案
这是一个例子:
setClass("yyy", representation(v="numeric"))
setMethod("+", signature(e1 = "yyy", e2 = "yyy"), function (e1, e2) e1@v + e2@v)
setMethod("+", signature(e1 = "yyy", e2 = "numeric"), function (e1, e2) e1@v + e2)
那么,
> y1 <- new("yyy", v = 1)
> y2 <- new("yyy", v = 2)
>
> y1 + y2
[1] 3
> y1 + 3
[1] 4
关于r - R 中 S4 对象的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8937719/