Data Structures – Matrix and Array in R

In the previous tutorial we saw atomic vectors and list. In this tutorial we look at Matrix and Array in R.

Array

An array is a vector with additional attributes
dim
which stores the dimension of the array and
dimnames
which stores the names of the dimensions. Here’s an example:

> array(1:8,dim=c(2,2,2))
, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8

This creates a 2,2,2 array. You can check if an object is an array by using the
is.array()
function

> x=array(1:8,dim=c(2,2,2))
> is.array(x)
[1] TRUE

The array has an attribute called dim.

> attributes(x)
$dim
[1] 2 2 2

You can also get the dimensions using the
dim
function

> dim(x)
[1] 2 2 2

we can assign names to the dimensions. In the example below we create a list with dimnames. The length of each element in the list should correspond to the dimension.

> x=array(1:8,dim=c(2,2,2),dimnames=list(c('a','b'),c('e','f'),c('g','h')))
> x
, , g

  e f
a 1 3
b 2 4

, , h

  e f
a 5 7
b 6 8


Matrix

A matrix is an 2 dimensional array. Lets create one

> a=matrix(1:8,nrow=2,ncol=4)
> a
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8

The data is filled column first. nrow specifies number of rows and ncol specifies number of columns. To fill in rows first use the ‘byrow’ specifier

> a=matrix(1:8,nrow=2,ncol=4,byrow=TRUE)
> a
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8

An element of a matrix can be accessed by specifying ‘row,column’

> a[2,3]
[1] 7

This retrieves the element at row 2 and column 3. You can also specify a single index

> a[1]
[1] 1
> a[2]
[1] 5

This accesses the elements in column first order. To select a complete row or complete column

# get first row
> a[1,] 
[1] 1 2 3 4
# get first column
> a[,1]
[1] 1 5

Multiple rows or columns can also be accessed

> a[,c(1,2)]
     [,1] [,2]
[1,]    1    2
[2,]    5    6
> a[,1:3]
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    5    6    7

You can assign names to the rows and columns. The names can be used to access the elements

> dimnames(a)=list(c("a","b"),c("c","d","e","f"))
> a
  c d e f
a 1 2 3 4
b 5 6 7 8
> a["a","e"]
[1] 3

You can check if an object is a matrix by using
is.matrix()
. Length of a matrix is given by
length()
. To find the number of rows and number of columns use the
nrow()
and
ncol()
functions respectively. To get all the dimensions use the
dim()
function.

> a=matrix(1:8,nrow=2,ncol=4,byrow=TRUE)
> length(a)
[1] 8
> dim(a)
[1] 2 4
> nrow(a)
[1] 2
> ncol(a)
[1] 4

To get all the names of the rows use the
rownames
function and to get all the column names use the
colnames
function.

> a=matrix(1:8,nrow=2,ncol=4,byrow=TRUE)
> dimnames(a)=list(c("a","b"),c("c","d","e","f"))
> rownames(a)
[1] "a" "b"
> colnames(a)
[1] "c" "d" "e" "f"

We can combine two matrices row wise using
rbind()
and columnwise using
cbind()
. we Will explore this function in details in the subsequent tutorials, but for now, lets look at one example

# create two matrices with dimensions 2x2
> a=matrix(1:4,ncol=2,nrow=2)
> a
     [,1] [,2]
[1,]    1    3
[2,]    2    4
> b=matrix(5:8,ncol=2,nrow=2)
> b
     [,1] [,2]
[1,]    5    7
[2,]    6    8

# cbind combines the matrices by columns. 
#think of it as putting the matrix on the side of the other matrix.
> cbind(a,b)
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8

#rbind combines the matrices by rows. 
#Think of it as putting the matrix on the bottom of the other matrix.
> rbind(a,b)
     [,1] [,2]
[1,]    1    3
[2,]    2    4
[3,]    5    7
[4,]    6    8

We can convert the matrix back to a vector by using the c() function

> c(a)
[1] 1 2 3 4
#as.vector also works
> as.vector(a)
[1] 1 2 3 4

A list can also be converted to a matrix.

> l = list("a",c(1,2),TRUE,"b")
> matrix(l,nrow=2,ncol=2)
     [,1]      [,2]
[1,] "a"       TRUE
[2,] Numeric,2 "b"
# we create a 2x2 matrix. 
> m=matrix(l,nrow=2,ncol=2)
# the 2,1 element is a vector
> m[2,1]
[[1]]
[1] 1 2

So far we have been using cases where we provide all elements while creating a matrix. Lets look at two examples, where we provide less elements than is required by the ncol and nrow specification.

# 6 elements and nrow*ncol = 6. all good.
> a=matrix(1:6,nrow=3,ncol=2)
> a
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
#3 elements and nrow*ncol = 6. 
# R recycles elements since number of elements is a multiple or 
#sub-multiple of number or rows.
> a=matrix(1:3,nrow=3,ncol=2)
> a
     [,1] [,2]
[1,]    1    1
[2,]    2    2
[3,]    3    3
# 4 elements and nrow*ncol = 6. Not good.
> a=matrix(1:4,nrow=3,ncol=2)
Warning message:
In matrix(1:4, nrow = 3, ncol = 2) :
  data length [4] is not a sub-multiple or multiple of the number of rows [3]

Leave a Reply

Your email address will not be published. Required fields are marked *