Study guide: R Matrices and Data Structures

Course Outline

  1. Creating and Indexing Matrices
  2. Vector and Matrix Operations
  3. Data Frames and Lists
  4. Logical Operators in R

1. Creating and Indexing Matrices

★ 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.

Memory Hook

[i,] selects a row; [,j] selects a column

2. Vector and Matrix Operations

★ 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.

  • The inverse of a matrix m is calculated in R with solve(m), and the matrix must be square for the inverse operation used in m1 %*% solve(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.

  • The expression m1 %*% solve(m2) is described as resembling m1 divided by m2 in matrix algebra, but it is not developed further.

3. Data Frames and Lists

Key Concepts & Definitions

  • Data frame: A structure similar to a matrix whose columns have names and may contain different data types.
  • List: A single object that collects several objects, including objects of different types.

Essential Points

  • 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.

Memory Hook

Data frames organize named columns; lists collect objects of different types

4. Logical Operators in R

★ Must-know

  • In R, & means logical AND, | means logical OR, == tests equality, and != tests inequality.

📌 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

  • The ! operator negates a logical value, and repeated negation reverses the logical value each time.

Memory Hook

& and | work element-wise; && and || inspect only the first element

Synthesis Tables

R Data Structures

StructureOrganizationData types
MatrixRows and columnsEntries share a common type
Data frameNamed columnsColumns may have different types
ListCollected objectsObjects may have different types

Test yourself

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?

Take the quiz →

Study with flashcards

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.

See the flashcards →

Related classes

Make your own study guides

Upload your class notes and Revizly writes a study guide, a quiz and flashcards in about a minute.

Study guide maker