R Data Types
R exposes a few atomic data types and many more derived data types.
Classes
Objects in the R language belong to one or more classes. These are inspected using the class() function.
> now <- Sys.time() > class(now) [1] "POSIXct" "POSIXt"
Numeric
There are principally two subtypes of numeric data:
integer data types store an integer value
double data types store a numeric value with decimal places
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.
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.char <- 1.23
Complex
The complex data type stores a complex numeric value.
my.char <- 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.char <- TRUE
Character
The character data type stores a string value.
my.char <- "R"
Vector
A vector is an ordered container whose members are of a uniform data type.
my.vector <- c(1, 2, 3)
Array
An array is another ordered container whose members are of a uniform data type.
my.array <- array(c(1, 2, 3, 4))
List
A list is an ordered container of any data types.
my.list <- list("R", 1.23, TRUE)
Matrix
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)
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 row's 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")
