R Matrices and Data Structures Practice Quiz (8 questions)

All questions and answers

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?

matrix(c(8, 5, 1, 4, 7, 9), 2, 3)
matrix(c(8, 5, 1, 4, 7, 9), 3, 2)
matrix(2, 3, c(8, 5, 1, 4, 7, 9))
c(8, 5, 1, 4, 7, 9)

matrix(c(8, 5, 1, 4, 7, 9), 2, 3)

Explanation

In `matrix(data, nrow, ncol)`, the first argument supplies the entries and the next two set the row and column counts. Reversing 2 and 3 swaps the dimensions, while the other choices misplace the arguments or do not create a matrix.

2. For a matrix `M` with at least 4 rows and 3 columns, which expression selects its entire fourth row?

M[,4]
M[4]
M[4,3]
M[4,]

M[4,]

Explanation

In matrix indexing, the form `[i,]` selects row `i`, so `M[4,]` returns the fourth row. `M[,4]` selects a column, `M[4,3]` selects a single entry, and `M[4]` uses a single index.

3. Two vectors have lengths 4 and 2. Which operation is permitted by the stated vector-dimension rules?

Adding them element by element
Dividing them element by element
Subtracting them element by element
Multiplying them with `%*%` when the matrix dimensions align

Multiplying them with `%*%` when the matrix dimensions align

Explanation

Vectors of different dimensions cannot be added, subtracted, or divided, but `%*%` can multiply vectors of different dimensions when their matrix dimensions align. The other choices apply same-dimension operations to unequal-length vectors.

4. Matrix `A` has 2 rows and 4 columns, while matrix `B` has 4 rows and 3 columns. Which operation is dimensionally valid?

Computing `A %*% B`, which produces a 2-by-3 matrix
Subtracting `A - B`, which produces a 2-by-4 matrix
Adding `A + B`, which produces a 2-by-4 matrix
Computing `B %*% A`, which produces a 4-by-4 matrix

Computing `A %*% B`, which produces a 2-by-3 matrix

Explanation

Matrix multiplication is valid when the number of columns of the first matrix matches the number of rows of the second: here both are 4, yielding a 2-by-3 result. Addition and subtraction require the matrices to have the same dimensions, and `B %*% A` has mismatched inner dimensions.

5. What is a data frame in R?

A structure with named columns that may contain different data types
A single object that collects several objects, potentially of different types
A vector whose elements are all stored using the same data type
A structure of rows and columns whose entries share a common data type

A structure with named columns that may contain different data types

Explanation

A data frame organizes data into named columns, and different columns may contain different data types. A list also can collect objects of different types, but it is not defined by named columns.

6. For vector comparisons in R, how does `&&` differ from `&`?

`&&` compares corresponding elements, while `&` checks the first element.
`&&` tests equality, while `&` tests inequality.
`&&` checks the first element, while `&` compares corresponding elements.
`&&` negates each element, while `&` combines vectors by position.

`&&` checks the first element, while `&` compares corresponding elements.

Explanation

The operator `&&` evaluates only the first element of each logical vector, whereas `&` evaluates element by element and returns a logical vector. The reversed interpretation is incorrect because it assigns the element-wise behavior to `&&`.

7. What is the purpose of `rownames(x)` when `x` is a matrix in R?

It assigns names to the matrix's rows.
It assigns names to the matrix's columns.
It selects a matrix column by its position.
It retrieves the matrix entry at a specified row and column.

It assigns names to the matrix's rows.

Explanation

`rownames(x)` assigns names to a matrix's rows, making them identifiable by labels. `colnames(x)` serves the related but different purpose of naming columns.

8. How does matrix multiplication differ from element-wise multiplication for matrices in R?

Matrix multiplication pairs corresponding entries in equally sized matrices; element-wise multiplication combines rows and columns when inner dimensions match.
Matrix multiplication adds corresponding entries; element-wise multiplication subtracts corresponding entries.
Matrix multiplication requires square matrices; element-wise multiplication permits matrices with different dimensions.
Matrix multiplication combines rows and columns when inner dimensions match; element-wise multiplication pairs entries in equally sized matrices.

Matrix multiplication combines rows and columns when inner dimensions match; element-wise multiplication pairs entries in equally sized matrices.

Explanation

With `%*%`, matrix products are valid when the left matrix's column count matches the right matrix's row count; element-wise multiplication is permitted for matrices with the same dimensions. The common-dimensions rule applies to element-wise operations, not to matrix multiplication.

Study with flashcards

Lock in the answers with 9 flashcards on R Matrices and Data Structures.

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 →

Read the study guide

Go over the full study guide for R Matrices and Data Structures.

Read the study guide →

Related classes

Make your own practice quizzes

Upload your class notes and Revizly writes a practice quiz with explained answers in about a minute.

Quiz maker