Matrices in R
Two-dimensional numeric data and matrix algebra.
m <- matrix(1:6, nrow = 2, byrow = TRUE)
dimnames(m) <- list(c("r1", "r2"), c("a", "b", "c"))
shape <- dim(m)
cell <- m[1, 2]
first_row <- m[1, ]
last_col <- m[, 3]
transposed <- t(m)
product <- m %*% transposed
scaled <- m * 10
How it works
matrixfills by column unlessbyrow = TRUE.m[i, j]indexes; omitting one takes a whole row or column.ttransposes and%*%multiplies matrices.
Keywords and builtins used here
cdimdimnameslistmatrixt
The run, in numbers
- Lines
- 11
- Characters to type
- 230
- Tokens
- 88
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 59 seconds.
Step 1 of 2 in Matrices & lists, step 15 of 20 in Language basics.