操纵S4类

时间:2013-09-29 23:05:04

标签: r s4

Example of the problem - class.a has slots 1, 2, 3 
and inherits from class c (4, and 5).  
The slots in class (1, 2, 3) are passed to function.b as variables. 
the output of function b is as class.c.  So now I have the following.



class.a 
   slot 1  a value
   slot 2  a value
   slot 3  a value
   slot 4  empty
   slot 5  empty

   class.c
   slot 4 a result
   slot 5 a result

class.c可以简单地合并到class.a中吗?如果是这样,怎么样?我搜索过文档和    我看过虚拟和超类。    我找不到任何关于合并课程的内容

以下是创建类的代码 -

setClass("BondDetails",
       representation(
       Cusip = "character",
       ID = "character",
       BondType = "character",
       Coupon = "numeric",
       IssueDate = "character",
       DatedDate = "character",
       StartDate = "character",
       Maturity = "character",
       LastPmtDate = "character",
       NextPmtDate = "character",
       Moody = "character",
       SP = "character",
       BondLab  = "character",
       Frequency = "numeric",
       BondBasis = "character",
       Callable = "character",
       Putable = "character",
       SinkingFund = "character"))

setClass("BondCashFlows",
     representation(
     Price = "numeric",
     Acrrued = "numeric",
     YieldToMaturity = "numeric",
     ModDuration = "numeric",
     Convexity = "numeric",
     Period = "numeric",
     PmtDate = "Date",
     TimePeriod = "numeric",
     PrincipalOutstanding = "numeric",  
     CouponPmt = "numeric",
     TotalCashFlow = "numeric"))

 setClass("BondTermStructure",
     representation(
     EffDuration = "numeric",
     EffConvexity = "numeric",
     KeyRateTenor = "numeric",
     KeyRateDuration = "numeric",
     KeyRateConvexity = "numeric"))

 setClass("BondAnalytics",
     contains = c("BondDetails","BondCashFlows", "BondTermStructure"))

BondDetails是存储的信息 BondCashFlows使用债券详细信息的输入计算 BondTermStructure使用债券详细信息和债券流量的输入计算

我需要将它们全部放入BondAnalytics中,这样我就可以创建一个输出方法 似乎无法将它们变成一个超类

1 个答案:

答案 0 :(得分:1)

取决于其他700行代码的作用...默认情况下,S4类的initialize方法是一个复制构造函数,其中未命名的参数作为基类的实例。也许你的意思是你有类似的东西(我的班级A,B,C不对应你的班级,但不知何故,命名似乎对我理解你的问题更有意义)

.A <- setClass("A", representation(a="numeric"))
.B <- setClass("B", representation(b="numeric"))
.C <- setClass("C", contains=c("A", "B"))

.A(...)是“A”类等自动生成的构造函数; .A(...)创建类“A”的新实例,然后使用新实例和参数initialize调用...(即复制构造函数)。一种可能性是你有一个“A”的实例和一个“B”的实例,你想要构造和实例“C”

a <- .A(a=1)
b <- .B(b=2)

> .C(a, b)              # Construct "C" from base classes "A" and "B"
An object of class "C"
Slot "a":
[1] 1

Slot "b":
[1] 2

另一种可能性是你已经有了一个“C”的实例,并且你想更新这些值以包含来自“B”实例的值

b <- .B(b=2)
c <- .C(a=1, b=3)

然后

> initialize(c, b)     # Copy c, replacing slots from 'B' with 'b'
An object of class "C"
Slot "a":
[1] 1

Slot "b":
[1] 2