2

When I call a is function over a single variable, I got the following information.

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

What does these information tell us? Why We have three different attributes, such as integer, double and numeric in [1],

and what does data.frameRowLabel mean here?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
user288609
  • 12,465
  • 26
  • 85
  • 127
  • What gets returned when you run `a`? – Phil Mar 03 '21 at 05:06
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What are you trying to accomplish by calling this function? – MrFlick Mar 03 '21 at 06:01
  • 1
    This can be reproduced with `is(1L)` – Ronak Shah Mar 03 '21 at 06:53

1 Answers1

2

Calling is(object, class2) without second argument gives us the class of the object (the first element) as well as the extends (the following elements),

a <- 1:10

class(a)
# [1] "integer"

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

corresponding to the class definition of "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

Checking the definitions of the other classes of is(a) reveals that "integer" is a subclass of them.

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

We may check if an object extends to a specific class.

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

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

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

Extend and mode are different things:

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

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

To test inheritance relationships between an object and a class we may use 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

Without second argument we get all the extends:

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

Using fullInfo=TRUE gives the complete definitions of all the is-relations. See SClassExtension-class for further information.

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

Some more information on the "data.frameRowLabels" class can be found in the documentation for package ‘methods’.

jay.sf
  • 60,139
  • 8
  • 53
  • 110