Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 24 / 68

Tabular data with matrices

This lesson covers using matrix in R, with the aim of handling data in table form.

The matrix() function expresses a grid with rows and columns — a matrix. You reach into it as m[row, column].

The sample code builds two rows by three columns with matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) and reaches a value with a comma-separated row and column, as in m[2, 3]. Note byrow = TRUE, which fills the values row by row.

A common early stumble is that byrow argument. Leave it out and the values are filled column by column, giving a matrix laid out differently from what you intended.

In professional work, matrix arithmetic — statistical calculation, representing image data — is one of the areas R is strongest in.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)