# ============================================================ # Matrices in R # R Foundations — Rverse Analytics # https://rverseanalytics.com/learn/matrices # ============================================================ # Matrices in R - a 2D grid of one type # 1. Fold a vector into rows and columns m <- matrix(1:12, nrow = 3) m # 2. Shape and indexing: [row, column] dim(m) m[2, 3] m[1, ] m[, 2] # 3. Maths is elementwise m * 10 m + m # 4. Whole-row and whole-column summaries rowSums(m) colMeans(m) t(m) # 5. Label rows and columns rownames(m) <- c("a", "b", "c") colnames(m) <- c("w", "x", "y", "z") m["b", "y"]