★ Must-know
In R, matrix(data, nrow, ncol) creates a matrix with the specified number of rows and columns, filling its entries from data.
In matrix indexing, [i,j] selects the entry at row i and column j, [i,] selects row i, and [,j] selects column j.
For a vector x, x[n] selects its nth element, x[m:n] selects elements m through n, x[c(k,m,n)] selects specified positions, and x[-c(3,5)] excludes positions 3 and 5.
Further detail
The command matrix(4, 3, 3) creates a 3-by-3 matrix whose entries are all 4.
In R, x[x>m & x<n] selects elements of x greater than m and less than n, while x[[n]] retrieves the element at position n.
The functions colnames(x) and rownames(x) assign names to a matrix's columns and rows, respectively.
[i,] selects a row; [,j] selects a column
★ Must-know
📌 Vectors of different dimensions cannot be added, subtracted, or divided, but they can be multiplied using %*% for matrix multiplication.
📌 Matrices of different dimensions cannot be added, subtracted, or divided, but m1 %*% m2 is valid when the number of columns of m1 equals the number of rows of m2.
Further detail
📌 Vectors can be multiplied with %*% when their dimensions permit the matrix product, as in a 3-element column vector multiplied by the transpose of a 2-element vector.
📌 Matrices can be added, subtracted, multiplied element-wise, or divided when they have the same dimensions.
A data frame can be created with data.frame(), and its columns can be named with colnames().
A column can be added to a data frame by assignment with the dollar notation, as in mydatafr$Col3 <- c("Mark","David","Erika").
When vectors are passed directly to data.frame(), their names and data types are retained, cbind() is unnecessary, and vectors of different lengths may not work well.
In R, mylist[n] selects the nth list element while retaining it as a list, and mylist[-c(2,4)] returns a list with the second and fourth elements excluded.
Assigning NULL to a list position, as in mylist[4] <- NULL, removes that element from the list.
Data frames organize named columns; lists collect objects of different types
★ Must-know
📌 On vectors, & and | evaluate element by element and return a logical vector, whereas && and || evaluate only the first element of each logical vector and are recommended for scalars.
Further detail
& and | work element-wise; && and || inspect only the first element
R Data Structures
| Structure | Organization | Data types |
|---|---|---|
| Matrix | Rows and columns | Entries share a common type |
| Data frame | Named columns | Columns may have different types |
| List | Collected objects | Objects may have different types |
Take a 8-question practice quiz on R Matrices and Data Structures, with every answer explained.
1. In R, you need a matrix with 2 rows and 3 columns populated from the vector `c(8, 5, 1, 4, 7, 9)`. Which command creates it with those dimensions?
2. For a matrix `M` with at least 4 rows and 3 columns, which expression selects its entire fourth row?
Memorize the key concepts of R Matrices and Data Structures with 9 flashcards.
What does `matrix(data, nrow, ncol)` create in R?
A matrix filled from `data` with the specified numbers of rows and columns.
What operations are permitted between vectors of different dimensions?
Matrix multiplication with `%*%` is permitted.
What does `matrix(4, 3, 3)` create in R?
A 3-by-3 matrix whose entries are all 4.
Upload your class notes and Revizly writes a study guide, a quiz and flashcards in about a minute.
Study guide maker