Skip to content

Contact sales

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

Programming R Matrices, Lists, and Arrays

Jul 22, 2020 • 10 Minute Read

Introduction

Working with data is an obvious requirement for data science professionals. The building blocks of working with data include understanding the most common data structures and how they are interrelated. In this guide, you will learn the techniques of programming matrices, lists, and arrays in R.

Vectors

It's important to understand the concept of vectors before moving ahead.

A vector is the most common data structure in R. It is a sequence of elements of the same basic type. The vector() function can be used to create a vector. The default mode is logical, but we can use constructors such as character(), numeric(), etc., to create a vector of a specific type.

The lines of code below construct a numeric and a logical vector, respectively. A vector can also contain strings, as shown by the vector s.

      n <- c(1,2,5.3,6,-2,4) 
l <- c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE) 
s = c("USA", "UK", "AFRICA", "INDIA", "CHINA") 

class(n)
class(l)
class(s)
    

Output:

      1] "numeric"
[1] "logical"
[1] "character"
    

It's also possible to do several operations on the vectors, such as combining vectors and performing mathematical operations. With this brief introduction to vectors, you are ready to understand matrices, lists, and arrays.

Matrices

In R, matrices are an extension of numeric or character vectors. All columns in a matrix must have the same mode and the same length. Also, as is the case with atomic vectors, the elements of a matrix must be of the same data type. The general representation of a matrix is shown in the code below.

The arguments nrow and ncol denote the number of rows and columns, respectively. The argument byrow = TRUE indicates that the matrix should be filled by the rows.

      m = matrix(c(20, 45, 33, 19, 52, 37), nrow=2, ncol=3, byrow = TRUE)    
print(m)
    

Output:

It is possible to identify the rows, columns, or elements of a matrix using subscripts. For example, the element at the second row and second column can be accessed using the following command.

      m[2, 2]
    

Output:

      1] 52
    

You can also create a matrix and give names to the rows and columns with the dimname argument. In the first matrix, m1, the elements are arranged sequentially by row. In the second matrix, m2, the arrangement is done by columns. The rownames and colnames specify the row and column names of the matrix. All these arguments are passed into the matrix() function while creating the matrix, m3.

      m1 <- matrix(c(21:32), nrow = 4, byrow = TRUE)
print(m1)

m2 <- matrix(c(21:32), nrow = 4, byrow = FALSE)
print(m2)

# Define the column and row names.
rownames = c("r1", "r2", "r3", "r4")
colnames = c("c1", "c2", "c3")

m3 <- matrix(c(21:32), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(m3)
    

Output:

You can program matrices to access the elements with row and column indices. For example, the code below prints the element at the third column and first row.

      print(m3[1,3])
    

Output:

      1] 23
    

If you want to access only the second row, the code below performs this task.

      print(m3[2,])
    

Output:

      c1 c2 c3 
24 25 26
    

It is possible to perform mathematical operations with matrices. The R operators are used to do this task, and the result is also a matrix, provided the number of rows and columns are the same for the matrices involved.

The code below creates a couple of two by three matrices and performs the addition operation. The resulting matrix is named combined, as shown below.

      score1 <- matrix(c(5, 9, 0, -2, 7, 6), nrow = 2)
score2 <- matrix(c(5, 2, 5, 9, -1, 4), nrow = 2)

combined <- score1 + score2
print(combined)
    

Output:

In the same manner, you can perform other mathematical operations on matrices, like subtraction, multiplication, and division.

Lists

A list is a generic vector containing a collection of objects (or components). The advantage of a list is that it allows you to store a variety of objects, which may be possibly unrelated, under one name.

The lines of code below create a list containing copies of three vectors: name, place, and age in years.

      name = c("abhi", "ansh", "ajay") 
place = c("delhi", "mumbai", "pune") 
age = c(TRUE, FALSE, TRUE, FALSE, FALSE) 

l = list(name, place, age)   
print(l)
    

Output:

      [1]]
[1] "abhi" "ansh" "ajay"

[[2]]
[1] "delhi"  "mumbai" "pune"  

[[3]]
[1]  TRUE FALSE  TRUE FALSE FALSE
    

You can merge several lists into one list as shown below.

      l1 <- list(10,20,30)
l2 <- list("Jan","Feb","March")
merged <- c(l1,l2)
print(merged)
    

Output:

      [1]]
[1] 10

[[2]]
[1] 20

[[3]]
[1] 30

[[4]]
[1] "Jan"

[[5]]
[1] "Feb"

[[6]]
[1] "March"
    

For programming purposes, you may be required to convert lists into vectors. This can be done with the unlist() function. This allows you to perform mathematical operations.

      l1 <- list(10,20,30)
l2 <- list(5,5,5)

v1 <- unlist(l1)
v2 <- unlist(l2)
print(v1)
print(v2)

addvec = v1 + v2
print(addvec)
    

Output:

      1] 10 20 30

[1] 5 5 5

[1] 15 25 35
    

Arrays

Arrays represent data objects that can store data in more than two dimensions. An array is created using the array() function. The lines of code below create an array, r1, that takes vectors vec1 and vec2 as inputs. It also uses the values in the dim parameter to create an array.

      vec1 <- c(50,20,40)
vec2 <- c(10,20,25,30,35,50)
r1 <- array(c(vec1,vec2),dim = c(3,3,2))
print(r1)
    

Output:

      , , 1

     [,1] [,2] [,3]
[1,]   50   10   30
[2,]   20   20   35
[3,]   40   25   50

, , 2

     [,1] [,2] [,3]
[1,]   50   10   30
[2,]   20   20   35
[3,]   40   25   50
    

The dimnames parameter can be used to give names to the rows, columns, and matrices in the array, as shown below.

      colnames <- c("column1","column2","column3")
rownames <- c("row1","row2","row3")
matrixnames <- c("matrix1","matrix2")

# Take these vectors as input to the array.
r2 <- array(c(vec1,vec2),dim = c(3,3,2),dimnames = list(rownames,colnames,
   matrixnames))
print(r2)
    

Output:

      , , matrix1

     column1 column2 column3
row1      50      10      30
row2      20      20      35
row3      40      25      50

, , matrix2

     column1 column2 column3
row1      50      10      30
row2      20      20      35
row3      40      25      50
    

It is easy to program and access array elements. The code below prints the third row of the second matrix of the array.

      print(r2[3,,2])
    

Output:

      column1 column2 column3 
     40      25      50
    

Similarly, the code below prints the element in the first row and third column of the first matrix.

      print(r2[1,3,1])
    

Output:

      1] 30
    

Conclusion

Deepika Singh

Deepika S.

Coming soon...

More about this author