Here, we will discuss matrices in R, how to create matrices, coerce matrices, and operations such as add, subtract, multiply and divide matrices.

A matrix contains homogeneous data types, hence, its composition can be only one of the numeric (or double), integer, character, logical, complex or raw data types. It has 1 or 2 dimensions, the rows (also the first dimension) are horizontal while the columns (also the second dimension) are vertical.

1 Create Matrices in R

Create a matrix with numeric (or double) elements:

mat1 = matrix(c(1, 2, 4, 8,
                16, 32, 64, 128,
                256, 512, 1024, 2048),
              nrow = 3, byrow = TRUE)
mat1
     [,1] [,2] [,3] [,4]
[1,]    1    2    4    8
[2,]   16   32   64  128
[3,]  256  512 1024 2048

Create a matrix with character elements:

mat2 = matrix(c(c("a", "b", "c"),
                c("x", "y", "z")),
              ncol = 2, byrow = FALSE)
mat2
     [,1] [,2]
[1,] "a"  "x" 
[2,] "b"  "y" 
[3,] "c"  "z" 

Name the rows and columns of a matrix:

rownames(mat2) = c("R1", "R2", "R3")
colnames(mat2) = c("C1", "C2")
mat2
   C1  C2 
R1 "a" "x"
R2 "b" "y"
R3 "c" "z"

Create an empty matrix:

mat3 = matrix(NA, nrow = 2, ncol = 3)
mat3
     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA

Create a diagonal matrix:

mat4 = diag(c(1, 2, 3), nrow = 3, ncol = 3)
mat4
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    2    0
[3,]    0    0    3

Transpose a matrix:

You can transpose a matrix with t(matrix).

mat5 = matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)
mat5
     [,1] [,2]
[1,]    1    2
[2,]    3    4
t(mat5)
     [,1] [,2]
[1,]    1    3
[2,]    2    4

Assign values to (or edit) some index of a matrix:

First number (or set of numbers) is for the rows (horizontal), while the second number (or set of numbers) is for the columns (vertical).

mat6 = matrix(NA, nrow = 2, ncol = 5)
mat6[,1] = 1
mat6[2,2] = 5
mat6[2,3:5] = 1000
mat6
     [,1] [,2] [,3] [,4] [,5]
[1,]    1   NA   NA   NA   NA
[2,]    1    5 1000 1000 1000
Summary of the Functions to Coerce or Check Matrices in R
Function Usage
dim() Check or set dimension
is.matrix() Check if matrix
typeof() Check data type
is.*type*() Check if matrix is of type
as.matrix() Coerce into matrix
as.*type*() Coerce matrix into type
  • type is numeric, integer, character, logical, or complex.

  • See examples below.

2 Check Matrices in R

Check the dimension of a matrix (# of rows, # of columns):

mat1 = matrix(c(1, 2, 4, 8,
                16, 32, 64, 128),
              nrow = 2, byrow = TRUE)
mat1
     [,1] [,2] [,3] [,4]
[1,]    1    2    4    8
[2,]   16   32   64  128
dim(mat1)
[1] 2 4

Change the dimension of a matrix (the new matrix will be filled by columns):

mat1 = matrix(c(1, 2, 3, 4,
                5, 6, 7, 8),
              nrow = 2, byrow = TRUE)
mat1
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
dim(mat1) = c(4, 2)
mat1
     [,1] [,2]
[1,]    1    3
[2,]    5    7
[3,]    2    4
[4,]    6    8

Check if an object is a matrix:

mat1 = matrix(c(1, 2, 4, 8), nrow = 2, byrow = TRUE)
mat1
     [,1] [,2]
[1,]    1    2
[2,]    4    8
is.matrix(mat1) # This will return TRUE or FALSE.
[1] TRUE

Check the data type or mode of a matrix:

mat1 = matrix(c(1, 2, 4, 8), nrow = 2, byrow = TRUE)
typeof(mat1)
[1] "double"
mat2 = matrix(c("A", "B", "C", "D"), nrow = 2, byrow = FALSE)
typeof(mat2)
[1] "character"

Check if a matrix’s components are of a type/mode:

mat1 = matrix(c(1, 2, 4, 8), nrow = 2, byrow = TRUE)
is.double(mat1)
[1] TRUE
is.integer(mat1)
[1] FALSE
mat2 = matrix(c("A", "B", "C", "D"), nrow = 2, byrow = FALSE)
is.character(mat2)
[1] TRUE

3 Coerce (into) Matrices in R

Coerce an object into a matrix:

This example turns a vector into a matrix.

vec1 = c(1, 2, 4, 8, 16, 32)
vec1
[1]  1  2  4  8 16 32
mat1 = as.matrix(vec1)
# Dimension of new matrix can be changed.
mat1
     [,1]
[1,]    1
[2,]    2
[3,]    4
[4,]    8
[5,]   16
[6,]   32

This example turns a dataframe into a matrix. Note that all elements become character type as a matrix only contains a homogeneous data type.

dtfrm1 = data.frame(Team = c("A", "B", "B"), 
                   Score = c(9, 8, 7))
dtfrm1
  Team Score
1    A     9
2    B     8
3    B     7
mat2 = as.matrix(dtfrm1)
mat2
     Team Score
[1,] "A"  "9"  
[2,] "B"  "8"  
[3,] "B"  "7"  

Coerce a matrix’s components into a different type/mode vector by columns:

mat1 = matrix(c(1, 2, 4, 8), nrow = 2, byrow = TRUE)
mat1
     [,1] [,2]
[1,]    1    2
[2,]    4    8
typeof(mat1)
[1] "double"
mat2 = as.integer(mat1)
mat2
[1] 1 4 2 8
typeof(mat2)
[1] "integer"
mat3 = as.character(mat1)
mat3
[1] "1" "4" "2" "8"
typeof(mat3)
[1] "character"

4 Add and Subtract Matrices in R

mat1 = matrix(c(1, 2, 3, 4), nrow = 2, byrow = FALSE)
mat1
     [,1] [,2]
[1,]    1    3
[2,]    2    4
mat2 = matrix(rep(1, times = 4), nrow = 2, byrow = FALSE)
mat2
     [,1] [,2]
[1,]    1    1
[2,]    1    1
# Add two matrices
mat1 + mat2
     [,1] [,2]
[1,]    2    4
[2,]    3    5
# Subtract a number from a matrix
mat1 - 1
     [,1] [,2]
[1,]    0    2
[2,]    1    3

5 Multiply and Divide Matrices in R

mat1 = matrix(c(10, 20, 30, 40), nrow = 2, byrow = FALSE)
mat1
     [,1] [,2]
[1,]   10   30
[2,]   20   40
mat2 = matrix(rep(10, times = 4), nrow = 2, byrow = FALSE)
mat2
     [,1] [,2]
[1,]   10   10
[2,]   10   10

Element by element matrix multiplication:

mat1 * mat2
     [,1] [,2]
[1,]  100  300
[2,]  200  400

Matrix multiplication:

mat1 %*% mat2
     [,1] [,2]
[1,]  400  400
[2,]  600  600

Divide matrix by a constant:

mat1 / 5
     [,1] [,2]
[1,]    2    6
[2,]    4    8

6 Join Strings from Character Matrices in R

For character matrices, you can use the paste() or paste0() (with no spacing) to join components:

mat1 = matrix(c("Apple", "Banana", "Cherry", "Date"),
              nrow = 2, byrow = FALSE)
mat2 = matrix(rep("Fruit", times = 4),
              nrow = 2, byrow = FALSE)
mat1; mat2
     [,1]     [,2]    
[1,] "Apple"  "Cherry"
[2,] "Banana" "Date"  
     [,1]    [,2]   
[1,] "Fruit" "Fruit"
[2,] "Fruit" "Fruit"
paste(mat1, mat2)
[1] "Apple Fruit"  "Banana Fruit" "Cherry Fruit" "Date Fruit"  
# paste0 will not add spacing when joining the strings
paste0(mat1, mat2)
[1] "AppleFruit"  "BananaFruit" "CherryFruit" "DateFruit"  
paste("This", mat1, "is a tasty fruit.")
[1] "This Apple is a tasty fruit."  "This Banana is a tasty fruit."
[3] "This Cherry is a tasty fruit." "This Date is a tasty fruit."  

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes