【问题标题】:How to interpret the output of is()?如何解释 is() 的输出?
【发布时间】:2021-06-01 16:26:18
【问题描述】:

当我对单个变量调用 is 函数时,我得到了以下信息。

> is(a)
[1] "integer"             "double"              "numeric"            
[4] "vector"              "data.frameRowLabels"

这些信息告诉我们什么?为什么我们有三个不同的属性,比如integer, double and numeric in [1]

data.frameRowLabel 在这里是什么意思?

【问题讨论】:

  • 运行a时返回什么?
  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。你想通过调用这个函数来完成什么?
  • 这个可以用is(1L)转载

标签: r class extend superclass


【解决方案1】:

在没有第二个参数的情况下调用is(object, class2) 会为我们提供对象(第一个元素)以及扩展名(以下元素)的class

a <- 1:10

class(a)
# [1] "integer"

is(a)
# [1] "integer"             "double"              "numeric"            
# [4] "vector"              "data.frameRowLabels"

对应"integer"的类定义。

getClassDef("integer")
# Class "integer" [package "methods"]
# 
# No Slots, prototype of class "integer"
# 
# Extends: 
# Class "double", directly, with explicit coerce
# Class "numeric", directly
# Class "vector", directly
# Class "data.frameRowLabels", directly
# 
# Known Subclasses: 
# Class "factor", from data part
# Class "ordered", by class "factor", distance 2

检查is(a) 的其他类的定义发现"integer" 是它们的子类。

selectSuperClasses("integer")  ## don't know why `"double"` isn't listed there...!
# [1] "numeric"             "vector"              "data.frameRowLabels"

getClassDef("data.frameRowLabels")
# Extended class definition ( "ClassUnionRepresentation" )
# Virtual Class "data.frameRowLabels" [package "methods"]
# 
# No Slots, prototype of class "character"
# 
# Known Subclasses: 
# Class "character", directly
# Class "integer", directly
# Class "signature", by class "character", distance 2
# Class "className", by class "character", distance 2
# Class "ObjectsWithPackage", by class "character", distance 2
# Class "factor", by class "integer", distance 2
# Class "ordered", by class "integer", distance 3

## also try:
getClassDef("numeric")
getClassDef("vector")
getClassDef("double")  ## lists `"integer"` as subclass

我们可能会检查一个对象是否扩展到特定的类。

is(a, "integer")
# [1] TRUE

is(a, "Date")
# [1] FALSE

## compare    
is(Sys.Date(), "Date")
# [1] TRUE

扩展和模式是不同的东西:

is(a, "double")
# [1] TRUE

## but
is.double(a)  ## tests the mode not the class resp. extend!
# [1] FALSE

为了测试对象和类之间的继承关系,我们可以使用extends(class1, class2, maybe = TRUE, fullInfo = FALSE)

extends("integer", "double")
# [1] TRUE

test1 <- c("integer", "double", "numeric", "vector", "data.frameRowLabels", 
           "character", "factor", "foo")
mapply(function(x, y) extends(y, x), test, "integer")
#             integer              double             numeric              vector 
#                TRUE                TRUE                TRUE                TRUE 
# data.frameRowLabels           character              factor                 foo 
#                TRUE               FALSE               FALSE               FALSE

没有第二个参数我们得到所有的扩展:

extends("integer")
# [1] "integer"             "double"              "numeric"            
# [4] "vector"              "data.frameRowLabels"

使用fullInfo=TRUE 给出了所有is 关系的完整定义。请参阅SClassExtension-class 了解更多信息。

ex <- extends("integer", fullInfo=TRUE)
str(ex)
# List of 5
# $ double             :Formal class 'SClassExtension' [package "methods"] with 10 slots
# .. ..@ subClass  : chr "integer"
# .. .. ..- attr(*, "package")= chr "methods"
# .. ..@ superClass: chr "double"
# .. .. ..- attr(*, "package")= chr "methods"
# .. ..@ package   : chr "methods"
# .. ..@ coerce    :function (from, strict = TRUE)  
#   .. ..@ test      :function (object)  
#     .. ..@ replace   :function (from, to, value)  
#       .. ..@ simple    : logi FALSE
# .. ..@ by        : chr(0) 
# .. ..@ dataPart  : logi FALSE
# .. ..@ distance  : num 1
# $ numeric            :Formal class 'SClassExtension' [package "methods"] with 10 slots
# .. ..@ subClass  : chr "integer"
# .. .. ..- attr(*, "package")= chr "methods"
# .. ..@ superClass: chr "numeric"
# .. .. ..- attr(*, "package")= chr "methods"
# .. ..@ package   : chr "methods"
# .. ..@ coerce    :function (from, strict = TRUE)  
#   .. ..@ test      :function (object)  
#     .. ..@ replace   :function (from, to, value)  
#       .. ..@ simple    : logi TRUE
# .. ..@ by        : chr(0) 
# .. ..@ dataPart  : logi FALSE
# .. ..@ distance  : num 1
# $ vector             :Formal class 'SClassExtension' [package "methods"] with 10 slots
# .. ..@ subClass  : chr "integer"
# .. .. ..- attr(*, "package")= chr "methods"
# .. ..@ superClass: chr "vector"
# .. .. ..- attr(*, "package")= chr "methods"
# .. ..@ package   : chr "methods"
# .. ..@ coerce    :function (from, strict = TRUE)  
#   .. ..@ test      :function (object)  
#     .. ..@ replace   :function (from, to, value)  
#       .. ..@ simple    : logi TRUE
# .. ..@ by        : chr(0) 
# .. ..@ dataPart  : logi FALSE
# .. ..@ distance  : num 1
# $ data.frameRowLabels:Formal class 'SClassExtension' [package "methods"] with 10 slots
# .. ..@ subClass  : chr "integer"
# .. .. ..- attr(*, "package")= chr "methods"
# .. ..@ superClass: chr "data.frameRowLabels"
# .. .. ..- attr(*, "package")= chr "methods"
# .. ..@ package   : chr "methods"
# .. ..@ coerce    :function (from, strict = TRUE)  
#   .. ..@ test      :function (object)  
#     .. ..@ replace   :function (from, to, value)  
#       .. ..@ simple    : logi TRUE
# .. ..@ by        : chr(0) 
# .. ..@ dataPart  : logi FALSE
# .. ..@ distance  : num 1
# $ integer            : logi TRUE

有关"data.frameRowLabels" 类的更多信息可以在documentation for package ‘methods’ 中找到。

【讨论】:

    猜你喜欢
    • 2014-08-09
    • 2014-02-03
    • 2018-09-16
    • 2018-09-09
    • 1970-01-01
    • 2015-11-11
    • 2016-08-18
    • 2011-09-14
    • 1970-01-01
    相关资源
    最近更新 更多