[英]How to interpret the output of is()?
當我在單個變量上調用 a is
function 時,我得到了以下信息。
> is(a)
[1] "integer" "double" "numeric"
[4] "vector" "data.frameRowLabels"
這些信息告訴我們什么? 為什么我們有三個不同的屬性,例如integer, double and numeric in [1]
,
data.frameRowLabel
在這里是什么意思?
在沒有第二個參數的情況下調用is(object, class2)
會給我們class
(第一個元素)的 class(第一個元素)以及 extends(以下元素),
a <- 1:10
class(a)
# [1] "integer"
is(a)
# [1] "integer" "double" "numeric"
# [4] "vector" "data.frameRowLabels"
對應於"integer"
的 class 定義。
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
我們可能會檢查 object 是否延伸到特定的 class。
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
為了測試 object 和 class 之間的 inheritance 關系,我們可以使用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"
class 的更多信息,請參閱package 'methods' 的文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.