Matrices in R

Interactive R tutorial on matrices: build one with matrix(), index rows and columns, run elementwise maths, and summarise with rowSums and colMeans β€” live in your browser.

A matrix is a two-dimensional grid holding one type of data. Edit and Run each box.

Watch: matrices in 90 seconds

Every step below β€” building, indexing and summarising a matrix β€” run live in RStudio, then try each one in the consoles that follow.

Download the R script  Β·  β–Ά Practice in the playground β€” or edit and run each snippet below.

Fold a vector into rows and columns

Shape and indexing: [row, column]

Leave one side empty to take a whole row or column:

Maths is elementwise

Whole-row and whole-column summaries

Label rows and columns

Once named, you can index by label instead of position:

Your turn


Next: Loops in R β†’ β€” repeat work with for and while.