如何将getter值转换为数据框,例如:
我有一个简单的类(人),它有2个对象(名称和人物),如果我想获得年龄值,我必须运行这个简单的指令“人[”年龄“]”我得到这个结果
An object of class "person"
Slot "val":
[1] 20 22 15 22 16
如何将其转换为数据框:
age
20
22
15
22
16
谢谢
这是输入结果(忘记其他类,人和人只是例子!)
new("Data"
, X = new("Signal"
, val = c(21, 22, 21, 22, 22, 24, 22, 23, 22, 22, 21)
)
, Y = new("Signal"
, val = c(11, 14, 13, 12, 12, 13, 12, 13, 14, 13, 13)
)
, Z = new("Signal"
, val = c(-130, -128, -129, -129, -129, -127, -128, -128, -128, -129,
-130)
)
)
这是setclass
.Signal.valid <- function(object){ return(TRUE)}
setClass (
Class ="Signal",
representation= representation(val="numeric"),
validity =.Signal.valid
)
rm (.Signal.valid )
答案 0 :(得分:0)
我不确定你的班级结构是否与我的相同,但我会试一试:
setClass("Signal", representation(val = "numeric"))
setClass("Data", representation(X = "Signal", Y = "Signal", Z = "Signal"))
obj <- new("Data", X = new("Signal", val = c(21, 22, 21, 22, 22, 24, 22, 23, 22, 22, 21)),
Y = new("Signal", val = c(11, 14, 13, 12, 12, 13, 12, 13, 14, 13, 13)),
Z = new("Signal", val = c(-130, -128, -129, -129, -129, -127, -128, -128, -128, -129, -130)))
data.frame(obj@X@val, obj@Y@val, obj@Z@val)
obj.X.val obj.Y.val obj.Z.val
1 21 11 -130
2 22 14 -128
3 21 13 -129
4 22 12 -129
5 22 12 -129
6 24 13 -127
7 22 12 -128
8 23 13 -128
9 22 14 -128
10 22 13 -129
11 21 13 -130
这是你想要实现的目标吗?