有谁知道R中的槽是什么?

我没有找到其含义的解释。我得到一个递归定义:“槽函数返回或设置有关对象的各个槽的信息”

感谢您的帮助,谢谢 - 小巷

有帮助吗?

解决方案

插槽链接到 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 编程的更多信息,请参阅 这个问题. 。如果这个概念对您来说仍然模糊,面向对象编程的一般介绍可能会有所帮助。

附:注意使用数据框和列表的区别 $ 访问命名变量/元素。

其他提示

正如 names(variable) 列出所有 $- 复杂变量的可访问名称,也是如此

slotNames(object) 列出对象的所有插槽。

非常方便地发现您的fit-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必须重新定义所有内容。大多数普通的编程语言称其为“属性”或“属性”。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top