Skip to content
Incomplete sheet

This sheet is incomplete and could use some attention. Please submit code snippet suggestions as an issue or PR here.

matrix

Create

Action Code Details
From vector, as column vector
matrix(x)
From vector, as column vector
cbind(x)
Diagonal matrix from vector
diag(x)
Create vector or matrix, depending on the number of columns
mat.or.vec(x, nc=2)

Test

Action Code Details
Is matrix
is.matrix(x)
Is vector (has singleton dimension)
?
Is square matrix
nrow(x) == ncol(x)
Each column contains at most 1 unique value (i.e. all equal)
apply(x, 2, uniqueN) == 1
Check if all rows are equal
all(mat[1,] == t(mat))

Get

Action Code Details
Elements by (row,col) pairs
mat[matrix(c(2,3), c(4,5)), ncol=2)]

Convert

Action Code Details
To list of row vectors
split(mat, row(mat))
To list of column vectors
split(mat, col(mat))
To list of submatrices grouped by row
lapply(split(mat, rowFactor), matrix, ncol = ncol(mat))