What's a matrix?/h2>

In R, a matrix is a collection of elements of the same data type (numeric, character, or logical) arranged into a fixed number of rows and columns. Since you are only working with rows and columns, a matrix is called two-dimensional.

You can construct a matrix in R with the matrix() function. Consider the following example:

matrix(1:9, byrow = TRUE, nrow = 3)

In the matrix() function:

Instruction

Construct a matrix with 3 rows containing the numbers 1 up to 9, filled row-wise.

# Construct a matrix with 3 rows that contain the numbers 1 up to 9 # Construct a matrix with 3 rows that contain the numbers 1 up to 9 matrix(1:9, byrow = TRUE, nrow = 3) test_function("matrix", c("data", "byrow", "nrow"), incorrect_msg = "Have you correctly created the matrix? Have a look at the assignment, the answer is already given!") test_output_contains("matrix(1:9, byrow=TRUE, nrow=3)", incorrect_msg = "There seems to be an issue with the matrix definition. Have a look at the assignment, the answer is already given!") success_msg("Great! Continue to the next exercise.")

Read the assignment carefully, the answer is already given!

Previous: 2-16 | Advanced selection

Next: 3-2 | Analyze matrices, you shall

Back to Main