Table of Contents

R - Matrix

About

The matrix object in R is an array of two dimensions with the same class (data type).

Constructor

You can create a matrix with three methods:

Matrix Function

matrix(data = NA, ncol = 1, nrow = length(data)/ncol, byrow = FALSE, dimnames = NULL)

where the attribute:

More:

?matrix

(Columns of Rows) Vector Binding

# v1, ..., vn becomes columns of the matrix
cbind(v1, ..., vn) 
# v1, ..., vn becomes rows of the matrix
rbind(v1, ..., vn) 

where:

Vector to matrix

With the following vector:

> x
[1] 1 2 3 4 5 6

> is.vector(x)
[1] TRUE

you can transform it as a matrix by applying dimensionality:

> dim(m)=c(2,3)
> m
[,1] [,2] [,3]
[1,]    1    2    3
[2,]   11   12   13

# is.matrix returns TRUE if x is a vector and has a "dim" attribute of length 2 and FALSE otherwise. 
> is.matrix(x)
[1] TRUE

Example

Matrix Constructor

Without Data

matrix(,3,3)
[,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA
[3,]   NA   NA   NA

Byrow

matrix(1:9,3,3,FALSE)
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

matrix(1:9,3,3,TRUE)
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

Dimnames

> dimnames = list(c("Row1", "Row2"), c("Col1", "Col2", "Col3"))
> m <- matrix(c(1,11,2,12,3,13), nrow = 2, ncol = 3, dimnames = dimnames)
Col1 Col2 Col3
Row1    1    2    3
Row2   11   12   13

dimensionality

default
matrix(1:6)
[,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5
[6,]    6

ncol

Matrix with only the number of columns

> m=matrix(1:6,ncol=2)
> m
[,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

Columns and rows binding

With the following two vectors:

> v1=c(1,2,3)
> v2=c(4,5,6)
> m=cbind(v1,v2)
> m
v1 v2
[1,]  1  4
[2,]  2  5
[3,]  3  6

* Matrix creation with Rows Binding

> m=rbind(v1,v2)
> m
[,1] [,2] [,3]
v1    1    2    3
v2    4    5    6

Operations

Mathematical

Matrix Operations

s = seq (1,4)
m=matrix(s,2,2)
m
[,1] [,2]
[1,]    1    3
[2,]    2    4

s2 = seq (4,1)
m2 = matrix(s2,2,2)
m2
[,1] [,2]
[1,]    4    2
[2,]    3    1

m*m2
[,1] [,2]
[1,]    4    6
[2,]    6    4

m-m2
[,1] [,2]
[1,]   -3    1
[2,]   -1    3

m/m2
[,1] [,2]
[1,] 0.2500000  1.5
[2,] 0.6666667  4.0

m^m2
[,1] [,2]
[1,]    1    9
[2,]    8    4

Subset

See R - Subset Operators (Extract or Replace Parts of an Object)

How to

Check if it's a Matrix

is.matrix(x)

Check the attributes

With the following matrix,

Col1 Col2 Col3
Row1    1    2    3
Row2   11   12   13

you can check the attributes with the attributes function.

> attributes(m)
$dim
[1] 2 3

$dimnames
$dimnames[[1]]
[1] "Row1" "Row2"

$dimnames[[2]]
[1] "Col1" "Col2" "Col3"

Get the numbers of rows and/of columns

> ncol(m)
[1] 3

> nrow(m)
[1] 2

Get the values of a cell

By indexing, you can retrieve values (by default as a vector).

m=matrix(1:6,ncol=2)
[,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

m[2,1]
[1] 2

m[2,]
[1] 2 5

 m[1, , drop = FALSE]
[,1] [,2]
[1,]    1    4

Change the dimensionality of a matrix

> m=matrix(1:6,ncol=2)
> m
[,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

> dim(m)=c(2,3)
> m
[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Remove the NA values

See R - NA (Not Available)