typestar

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

  1. matrix fills by column unless byrow = TRUE.
  2. m[i, j] indexes; omitting one takes a whole row or column.
  3. t transposes and %*% multiplies matrices.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 2 in Matrices & lists, step 15 of 20 in Language basics.

← Previous Next →