|
⇤ ← Revision 1 as of 2026-04-07 21:11:47
Size: 2806
Comment: Initial commit
|
← Revision 2 as of 2026-04-07 21:21:16 ⇥
Size: 3312
Comment: Corrections and style standardizations
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 13: | Line 13: |
| Objects in the R language belong to one or more classes. These are inspected using the `class()` function. | Objects in the R language belong to one or more non-exclusive classes. These are inspected using the `class()` function. |
| Line 21: | Line 21: |
| Line 25: | Line 26: |
| == Numeric == | == Modes == |
| Line 27: | Line 28: |
| 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 |
Incidentally, the atomic data types can be categorized by their '''mode''', which essentially refers to the underlying storage mechanism. |
| Line 31: | Line 30: |
| 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. | 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. |
| Line 36: | Line 41: |
| my.char <- 1.23 | > 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" |
| Line 43: | Line 86: |
| == Complex == | == Compositions == |
| Line 45: | Line 88: |
| The '''complex''' data type stores a complex numeric value. | A '''vector''' is an ordered container whose members are of a uniform data type. |
| Line 48: | Line 91: |
| my.char <- 9i + 3 | > my.vector <- c(1, 2, 3) |
| Line 51: | Line 94: |
| 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`. |
An '''array''' is another ordered container whose members are of a uniform data type. |
| Line 62: | Line 97: |
| my.char <- TRUE | > 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) |
| Line 69: | Line 116: |
| == Character == The '''character''' data type stores a string value. {{{ my.char <- "R" }}} ---- |
== Other Common Objects == |
| Line 81: | Line 120: |
| == Vector == | === Factor === |
| Line 83: | Line 122: |
| A '''vector''' is an ordered container whose members are of a uniform data type. | 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. |
| Line 86: | Line 125: |
| 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) |
> my.factor <- as.factor(my.factor) |
| Line 142: | Line 133: |
| my.factor <- relevel(my.factor, ref="Label") | > my.factor <- relevel(my.factor, ref="Label") |
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")
