R Data Types
R exposes a few atomic data types and many more derived data types.
Contents
Classes
Objects in the R language belong to one or more non-exclusive classes. These are inspected using the class() function.
> now <- Sys.time() > class(now) [1] "POSIXct" "POSIXt"
Modes
Incidentally, the atomic data types can be categorized by their mode, which essentially refers to the underlying storage mechanism.
The mode of an object can be inspected using the mode() function. Note that the typeof() function is nearly equivalent, with one exception noted below.
Numeric
Numeric is a mode of data types.
Data types can be explicitly cast into a numeric type using the as.integer(), as.double(), and as.numeric() functions. Similarly, use the is.integer(), is.double(), and is.numeric() functions to test for these specific data types.
> my.num <- 1.23
There are principally two subtypes of numerics, and these are what the typeof() function will return instead of the mode.
integer data types store an integer value
double data types store a numeric value with decimal places
For a number of reasons, it usually does not matter which subtype is used. Most implementations of algorithms and estimations implicitly use double precision, not arbitrary precision. Furthermore, implicitly casting between numeric data types is generally an acceptable side effect in implementations.
Complex
The complex data type stores a complex numeric value.
> my.com <- 9i + 3
Complex values are not numeric in R. Calling is.numeric() on a complex value will return FALSE.
Logical
The logical data type stores a Boolean value. These are represented as the keywords TRUE and FALSE.
> my.bool <- TRUE
Character
The character data type stores a string value.
> my.char <- "R"
Compositions
A vector is an ordered container whose members are of a uniform data type.
> my.vector <- c(1, 2, 3)
An array is another ordered container whose members are of a uniform data type.
> my.array <- array(c(1, 2, 3, 4))
A list is an ordered container of any data types.
> my.list <- list("R", 1.23, TRUE)A matrix is a multi-dimensional container whose members are of a uniform data type.
> my.matrix <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2)
Other Common Objects
Factor
A factor is a data type that represents categorical levels. For one, this is an optimization for storage; the vector of unique levels is stored, then all rows' values are replaced with integers that refer to an index in that vector.
> my.factor <- as.factor(my.factor)
to inspect the levels of a factor, try the levels() function.
When regressing on a factor, dummy coding is automatically applied. To force a reference level, try:
> my.factor <- relevel(my.factor, ref="Label")
