R中S4个对象的总和 [英] sum of S4 objects in R

查看:98
本文介绍了R中S4个对象的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个S4类,我想定义这些对象的线性组合.

I have a S4 class and I would like to define the linear combination of these objects.

是否可以在此特定类上分派*+函数?

Is it possible to dispatch * and + functions on this specific class?

推荐答案

下面是一个示例:

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中S4个对象的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆