Jor*_*eys 81
插槽链接到S4对象.槽可以被视为对象的一部分,元素或"属性".假设你有一个汽车对象,那么你可以拥有"价格","门数","发动机类型","里程"等插槽.
在内部,它代表一个列表.一个例子 :
setClass("Car",representation=representation(
price = "numeric",
numberDoors="numeric",
typeEngine="character",
mileage="numeric"
))
aCar <- new("Car",price=20000,numberDoors=4,typeEngine="V6",mileage=143)
> aCar
An object of class "Car"
Slot "price":
[1] 20000
Slot "numberDoors":
[1] 4
Slot "typeEngine":
[1] "V6"
Slot "mileage":
[1] 143
Run Code Online (Sandbox Code Playgroud)
这里,价格,numberDoors,typeEngine和里程数是S4类"Car"的插槽.这是一个简单的例子,实际上插槽本身可以是复杂的对象.
可以通过多种方式访问插槽:
> aCar@price
[1] 20000
> slot(aCar,"typeEngine")
[1] "V6"
Run Code Online (Sandbox Code Playgroud)
或通过构建特定方法(参见额外文档).
有关S4编程的更多信息,请参阅此问题.如果这个概念对你来说仍然含糊不清,那么面向对象编程的一般性介绍可能会有所帮助.
PS:注意与数据框和列表的区别,$
用于访问命名变量/元素.
tim*_*tim 16
就像names(variable)
列出$
复杂变量的所有可访问名称一样,也是如此
slotNames(object)
列出对象的所有插槽.
非常方便您发现您的健康物品包含哪些物品,以满足您的观赏乐趣.
Rei*_*son 10
除了@Joris指出的资源,加上他自己的答案,请尝试阅读?Classes
,其中包括以下插槽:
Slots:
The data contained in an object from an S4 class is defined
by the _slots_ in the class definition.
Each slot in an object is a component of the object; like
components (that is, elements) of a list, these may be
extracted and set, using the function ‘slot()’ or more often
the operator ‘"@"’. However, they differ from list
components in important ways. First, slots can only be
referred to by name, not by position, and there is no partial
matching of names as with list elements.
....
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27362 次 |
最近记录: |