[,1] [,2]
[1,] 1 3
[2,] 2 4
[,1] [,2]
[1,] TRUE TRUE
[2,] FALSE FALSE
Lecture 04
R supports the creation of 2d matrix data structures using atomic vector types.
Generally these are formed via a call to matrix().
Matrices in R use column major ordering (data is stored by column).
Matrices (and arrays) are just atomic vectors with a dim attribute attached. They do not have an explicit class attribute, but have implicit class(es).
report = function(x) {
UseMethod("report")
}
report.default = function(x) {
paste0("Class ", class(x)," not supported.")
}
report.double = function(x) {
"I'm a double!"
}
report.numeric = function(x) {
"I'm a numeric!"
}
report.matrix = function(x) {
"I'm a matrix!"
}
report.array = function(x) {
"I'm an array!"
}Arrays are just an \(n\)-dimensional extension of matrices and are defined by adding the appropriate dimension sizes.
class()A 2d array will have class c("matrix","array") while 1d or >2d will only have class "array"
A data frame is how R handles heterogeneous tabular data (i.e. a table of rows and columns) and is one of the most commonly used data structure in R.
R represents data frames using a list of equal length vectors.
Previous to R v4.0, the default behavior of data frames was to convert character data into factors. Sometimes this was useful, but mostly it wasn’t.
This behavior is controlled via the stringsAsFactors argument to data.frame (and related functions like read.csv, read.table, etc.).
When creating a data frame from different vectors, the lengths of the component vectors will be coerced to match. However, if they are not multiples of each other then there will be an error (other previous forms of length coercion would produce a warning for this case).
R has three subsetting operators ([, [[, and $). The behavior of these operators depends on the object (class) they are being used with (S3).
In general there are 6 different types of subsetting that can be performed based on the value passed to the operator,
Positive integer
Negative integer
Logical value
Empty / NULL
Zero valued
Character value (names)
Returns elements at the given location(s)
Excludes elements at the given location(s)
Returns elements that correspond to TRUE in the logical vector. Length of the logical vector is coerced to be the same as the vector being subsetted.
Returns the original vector, this is not the same as subsetting with NULL
Returns an empty vector (of the same type), this is the same as subsetting with NULL
If the vector has names, selects 1st element whose names correspond to the value in the names attribute.
This final type of subsetting follows the rules for length coercion with a 0-length vector (i.e. the vector being subset gets coerced to having length 0 if the subsetting vector has length 0)
Subsets can also be used with assignment to update specific values within an object (in-place).
[[ and $[[ subsets like [ except it can only subset for a single value
Subsets a single value, but returns the value - not a list containing that value. Multiple values are interpreted as nested subsetting.
$ is equivalent to [[ but it only works for name based subsetting of lists (it also uses partial matching for names)
Why does the following code not work?
The expression x$y gets interpreted as x[["y"]] by R, note the inclusion of the "s, this is not the same as the expression x[[y]].
As data frames have 2 dimensions, we can subset on either the rows or the columns - the subsetting values are separated by a comma.
Most of the time, R’s [ subset operator is a preserving operator, in that the returned object will always have the same type/class as the object being subset.
Confusingly, when used with some classes (e.g. data frame, matrix or array) [ becomes a simplifying operator (does not preserve type) - this behavior is instead controlled by the drop argument.
drop only works when the resulting value can be represented as a 1d vector (either a list or atomic).
| Type | Simplifying | Preserving |
|---|---|---|
| Atomic Vector | x[[1]] |
x[1] |
| List | x[[1]] |
x[1] |
| Matrix / Array | x[[1]] x[1, ] x[, 1] |
x[1, , drop=FALSE] x[, 1, drop=FALSE] |
| Factor | x[1:4, drop=TRUE] |
x[1:4] x[[1]] |
| Data frame | x[, 1] x[[1]] |
x[, 1, drop=FALSE] x[1] |
Sta 323 - Spring 2026