有谁知道R中的插槽是什么?
我没有找到它的含义的解释。我得到一个递归定义:“插槽函数返回或设置有关对象的各个插槽的信息”
帮助将不胜感激,谢谢 - Alley
插槽链接到 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
在这里,price、numberDoors、typeEngine 和mileage 是S4 类“Car”的插槽。这是一个简单的例子,实际上槽本身也可以是复杂的对象。
插槽可以通过多种方式访问:
> aCar@price
[1] 20000
> slot(aCar,"typeEngine")
[1] "V6"
或通过特定方法的构造(请参阅额外的文档)。
有关 S4 编程的更多信息,请参阅此问题。如果这个概念对您来说仍然很模糊,那么面向对象编程的一般介绍可能会有所帮助。
PS:注意数据框和列表的区别,您可以$
在其中访问命名变量/元素。
就像names(variable)
列出$
复杂变量的所有 -accessible 名称一样,
slotNames(object)
列出对象的所有插槽。
非常方便地发现您的适合对象包含哪些好东西以供您观赏。
除了@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.
....
不知道为什么 R 必须重新定义一切。大多数普通的编程语言称它们为“属性”或“属性”。