Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Matrices, Lists, and Arrays in R

Apr 22, 2020 • 11 Minute Read

Introduction

In this guide, we are going to learn about three of the most common datastructures in R. These datastructures will be familiar from your math studies, and they are also present in every programming language with very little difference regarding the implementation. These datastructures are matrices, which are two-dimensional verctors, lists, which are one-dimensional vectors or special objects that can hold items with different types, and arrays, which are vectors with one or more dimensions. First the basic concept of each of these data containers will be introduced, and then we will look at their practical use cases. At the heart of each datastructure, the atomic vector datatype is hidden.

Matrices

Matrices are nothing more than a collection of data elements arranged in a rectangular layout that is two-dimensional. An example matrix with 3x3 dimensions looks like this.

The most important thing you need to remember to get started with matrices is the matrix() function. This function has the following skeleton.

      matrix( 
   c(), 
   nrow=,
   ncol=,
   byrow = )
    

The first argument is a vector that defines which atomic values are present in the matrix. The second argument defines how many rows that vector splits up, and the third argument tells how many columns. The number of elements in the vector should be multiple or sub-multiple to nrow * ncol. The last argument defines whether you want to fill up the matrix by rows or columns. By default, the argument for byrow is FALSE, which means the matrix if filled up from column to column.

Let's try this one out. Your matrix definition looks like this.

      matrix(
c(1,2,3,4,5,6,7,8),
nrow = 4,
ncol = 2,
byrow = TRUE)
    

The output should look like this.

If you omit the byrow=TRUE argument, the following output greets you.

Where did the rest of the elements go? The problem is that your vector is bigger than the matrix size. If you want to get all the values from the vector, the ncol=4 should be the modification you make. That way you have the following output.

Now that you have the foundations, how do you proceed?

Transpose

This concept comes from linear algebra. Basically, what happens is that the matrix gets flipped over its diagonal. In order to do this in R, you can use the t() function. Let's see how it looksgiven the matrix below.

      a <- matrix(
c(1,2,3,4,5,6,7,8),
nrow = 4,
ncol = 2,
byrow = TRUE)
    

Before transposing, the output looks like this.

After transposing, the output looks like this.

Combine Matrices

In order to combine two matrices, the cbind() function needs to be used. It takes two matrices as arguments and produces their combination. When you are combining matrices, you need to make sure the they have the same number of rows, otherwise an exception is thrown.

Given are two matrices B and D.

      B <- matrix( 
c(2, 4, 3, 1, 5, 7), 
nrow=3, 
ncol=2) 

D <-  matrix( 
c(1, 3, 2), 
nrow=3, 
ncol=1)
    

Their combination can be created the following way.

      cbind(B,D)
    

The output looks like this.

Deconstruction

This concept allows you to break down the matrix into its original vector, which can come handy in certain situations. Take the following matrix called H.

      H <-  matrix( 
c(1,2,3,4,5,6,7,8,9,10), 
nrow=5, 
ncol=2)
    

You are able to deconstruct it with the c() function.

      c(H)
    

The output looks like this.

      1]  1  2  3  4  5  6  7  8  9 10
    

Lists

Lists are objects that may contain elements of different types, similar to vectors. These different types can be of strings, numbers, vectors, and even another list inside. You can have matrices as different elements in your lists. The concept is a general container for special use cases. The function that allows you to create a list is called list().

An example list would look like this.

      data <- list("Server","Network Device",c(1,2,3,4), FALSE, list(1,2,3,4,5,6))
    

The content of your list is now as follows.

      [1]]
[1] "Server"

[[2]]
[1] "Network Device"

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

[[4]]
[1] FALSE

[[5]]
[[5]][[1]]
[1] 1

[[5]][[2]]
[1] 2

[[5]][[3]]
[1] 3

[[5]][[4]]
[1] 4

[[5]][[5]]
[1] 5

[[5]][[6]]
[1] 6
    

You can see that there is not really any limit as to how many or what type of elements you can store in the list. There is a special function called names() that allows you to name your list, which results in a special dictionary-like datastructure. A dictionary datastructure consists of a key-value pair. In this case, the key is the list of names and the values are the actual elements.

Let's give names to the list elements.

      names(data) <- c("Hardware", "Network", "vector", "boolean","nestedlist")
    

After the function is executed, we can refer to the elements in the list by their names.

      data$Hardware
[1] "Server"

data$Network
[1] "Network Device"

data$nestedlist
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] 6
    

This allows you to build more sophisticated functions and create abstractions that allow users to understand and maintain the code more efficiently. As with lists in other programming languages, you can access, manipulate, and merge the lists. The indexing starts from 1.

In order to access the elements, refer to them with their indexes.

Let's retrieve the first and second elements.

      > data[1]
$Hardware
[1] "Server"

> data[2]
$Network
[1] "Network Device"
    

In order to remove a specific element, assign the NULL value to its index. This will reduce the length of your list.

Let's remove the nested list. You can do this in two ways. The second one will only work if you have named your list elements.

      data[5] <- NULL

data$nestedlist <- NULL
    

Suppose you have two lists from different datasources, and you have a function that needs data from both of them. You have the option to merge these two lists.

      monthids <- list(1,2,3,4,5,6,7,8,9,10,11,12)
months <- list("Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec")
    

The way to achieve this is to use the c() function.

      merged.list <- c(monthids,months)
    

This will produce the following results.

      [1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] 6

[[7]]
[1] 7

[[8]]
[1] 8

[[9]]
[1] 9

[[10]]
[1] 10

[[11]]
[1] 11

[[12]]
[1] 12

[[13]]
[1] "Jan"

[[14]]
[1] "Feb"

[[15]]
[1] "Mar"

[[16]]
[1] "Apr"

[[17]]
[1] "May"

[[18]]
[1] "June"

[[19]]
[1] "July"

[[20]]
[1] "Aug"

[[21]]
[1] "Sep"

[[22]]
[1] "Oct"

[[23]]
[1] "Nov"

[[24]]
[1] "Dec"
    

The unlist() function allows you to convert your lists to vectors.

      myvector <- unlist(merged.list)
    

After this, all the usual arithmetic operators can be applied to the newly created vector.

Arrays

An array is a vector with one or more dimensions. A one-dimensional array can be considered a vector, and an array with two dimensions can be considered a matrix. Behind the scenes, data is stored in a form of an n-dimensional matrix. The array() function can be used to create your own array. The only restriction is that arrays can only store data types.

You can create a simple array the following way.

      v1 <- c(1,2,3)
v2 <- c(4,5,6,7,8,9)
result <- array(c(v1,v2),dim = c(3,3,2))
    

Now the result holds an array which has two matrices with three rows and three columns.

      , , 1

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

, , 2

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
    

The keyword here is dim. It defines the maximum number of indices in each dimension.

There is a more general syntax that is a skeleton to keep in mind and comes in handy most of the time.

      my_array <- array(data, dim = (rows, colums, matrices, dimnames)
    

You have the option to name your rows, columns and matrices in an array. Suppose you extend your above code with the following.

      v1 <- c(1,2,3)
v2 <- c(4,5,6,7,8,9)
col.names <- c("Item","Serial","Size")
row.names <- c("Server","Network","Firewall")
matrix.names <- c("DataCenter EU","DataCenter US")
result <- array(c(v1,v2),dim = c(3,3,2),dimnames = list(row.names,col.names,matrix.names))
    

Now the result array holds a more meaningful name that makes the code cleaner and easier to maintain.

      , , DataCenter EU

         Item Serial Size
Server      1      4    7
Network     2      5    8
Firewall    3      6    9

, , DataCenter US

         Item Serial Size
Server      1      4    7
Network     2      5    8
Firewall    3      6    9
    

Accessing the elements is a bit more tricky, but once you get the hang of it, it should become easy. The skeleton code you should keep in mind is the following.

      result[row,column,matrix]
    

There is a neat trick with this. If you omit any of the arguments, they will be collected for all matrices, rows, or columns.

For example, if you were to collect the serials from each datacenter, all you would have to do is write the following.

      result[1,2,]
    

The output should be the following.

      DataCenterEU DataCenterUS 
           4            4
    

If you were to collect the size of each device from every datacenter the following code would do the job.

      result[,3,]
    

The output should be the following.

      DataCenterEU DataCenterUS
Server              7            7
Network             8            8
Firewall            9            9
    

Arrays allow you to create matrices from them with the following code. Let's separate each datacenter to their own matrix.

      DCEU <- result[,,1]
DCUS <- result[,,2]
    

The corresponding outputs will be as you expect.

      #DCEU
         Item Serial Size
Server      1      4    7
Network     2      5    8
Firewall    3      6    9
#DCUS
         Item Serial Size
Server      1      4    7
Network     2      5    8
Firewall    3      6    9
    

Conclusion

This guide covered three crucial datastructures that are used by statistical analysts and other data-mining folks. We built up the foundations to understand how these datastructures build on each other and looked at practical examples of how we can manipulate their contents. I hope this guide has been informative to you, and I would like to thank you for reading it!

Dániel Szabó

Dániel Szabó

Written content author.

More about this author