A comprehensive survey of the types of things in R; 'mode' and 'class' and 'typeof' are insufficient
I agree that the type system in R is rather weird. The reason for it being that way is that it has evolved over (a long) time...
Note that you missed one more type-like function, storage.mode
, and one more class-like function, oldClass
.
So, mode
and storage.mode
are the old-style types (where storage.mode
is more accurate), and typeof
is the newer, even more accurate version.
mode(3L) # numeric
storage.mode(3L) # integer
storage.mode(`identical`) # function
storage.mode(`if`) # function
typeof(`identical`) # closure
typeof(`if`) # special
Then class
is a whole different story. class
is mostly just the class
attribute of an object (that's exactly what oldClass
returns). But when the class attribute is not set, the class
function makes up a class from the object type and the dim attribute.
oldClass(3L) # NULL
class(3L) # integer
class(structure(3L, dim=1)) # array
class(structure(3L, dim=c(1,1))) # matrix
class(list()) # list
class(structure(list(1), dim=1)) # array
class(structure(list(1), dim=c(1,1))) # matrix
class(structure(list(1), dim=1, class='foo')) # foo
Finally, the class can return more than one string, but only if the class attribute is like that. The first string value is then kind of the main class, and the following ones are what it inherits from. The made-up classes are always of length 1.
# Here "A" inherits from "B", which inherits from "C"
class(structure(1, class=LETTERS[1:3])) # "A" "B" "C"
# an ordered factor:
class(ordered(3:1)) # "ordered" "factor"
Types and classes of variables
In R every "object" has a mode
and a class
. The former represents how an object is stored in memory (numeric, character, list and function) while the later represents its abstract type. For example:
d <- data.frame(V1=c(1,2))
class(d)
# [1] "data.frame"
mode(d)
# [1] "list"
typeof(d)
# list
As you can see data frames are stored in memory as list
but they are wrapped into data.frame
objects. The latter allows for usage of member functions as well as overloading functions such as print
with a custom behavior.
typeof
(storage.mode
) will usually give the same information as mode
but not always. Case in point:
typeof(c(1,2))
# [1] "double"
mode(c(1,2))
# [1] "numeric"
The reasoning behind this can be found here:
The R specific function typeof returns the type of an R object
Function mode gives information about the mode of an object in the sense of Becker, Chambers & Wilks (1988), and is more compatible with other implementations of the S language
The link that I posted above also contains a list of all native R basic types
(vectors, lists etc.) and all compound objects
(factors and data.frames) as well as some examples of how mode
, typeof
and class
are related for each type.
Ex: Computing a vector of logicals that is true when x[i]0
Long but hopefully easy to understand.
for(i in 1:length(x)){
if(x[i] > 0){
y[i] <- TRUE
} else {
y[i] <- FALSE
}
}
How to create a vector of integers with structure()?
try this:
> x <- structure(c(1L,2L))
> is.integer(x)
[1] TRUE
but just
> x <- c(1L,2L)
> is.integer(x)
[1] TRUE
is ok.
Why does class() return not the same inside Map()?
You apply (map) all elements of an empty matrix to a function returning it's class. The class of an empty thing is NA
. A vector only containing NAs is of class logical in R.
Related Topics
What Is the Purpose of Setting a Key in Data.Table
Clang-7: Error: Linker Command Failed With Exit Code 1 For Macos Big Sur
Scatterplot With Marginal Histograms in Ggplot2
How to Number/Label Data-Table by Group-Number from Group_By
How to Change the Default Library Path For R Packages
Merge Two Data Frames While Keeping the Original Row Order
Which Data.Table Syntax For Left Join (One Column) to Prefer
Efficiently Generate a Random Sample of Times and Dates Between Two Dates
Select First and Last Row from Grouped Data
Extracting the Last N Characters from a String in R
Construct a Manual Legend For a Complicated Plot
Multiple Plots in For Loop Ignoring Par
Splitting a Continuous Variable into Equal Sized Groups
Dplyr Filter: Get Rows With Minimum of Variable, But Only the First If Multiple Minima
Why Do I Get "Warning Longer Object Length Is Not a Multiple of Shorter Object Length"