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.
Create a matrix with numeric (or double) elements:
[,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:
[,1] [,2]
[1,] "a" "x"
[2,] "b" "y"
[3,] "c" "z"
Name the rows and columns of a matrix:
C1 C2
R1 "a" "x"
R2 "b" "y"
R3 "c" "z"
Create an empty matrix:
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
Create a diagonal matrix:
[,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)
.
[,1] [,2]
[1,] 1 2
[2,] 3 4
[,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).
[,1] [,2] [,3] [,4] [,5]
[1,] 1 NA NA NA NA
[2,] 1 5 1000 1000 1000
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.
Check the dimension of a matrix (# of rows, # of columns):
[,1] [,2] [,3] [,4]
[1,] 1 2 4 8
[2,] 16 32 64 128
[1] 2 4
Change the dimension of a matrix (the new matrix will be filled by columns):
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[,1] [,2]
[1,] 1 3
[2,] 5 7
[3,] 2 4
[4,] 6 8
Check if an object is a matrix:
[,1] [,2]
[1,] 1 2
[2,] 4 8
[1] TRUE
Check the data type or mode of a matrix:
[1] "double"
[1] "character"
Check if a matrix’s components are of a type/mode:
[1] TRUE
[1] FALSE
[1] TRUE
Coerce an object into a matrix:
This example turns a vector into a matrix.
[1] 1 2 4 8 16 32
[,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.
Team Score
1 A 9
2 B 8
3 B 7
Team Score
[1,] "A" "9"
[2,] "B" "8"
[3,] "B" "7"
Coerce a matrix’s components into a different type/mode vector by columns:
[,1] [,2]
[1,] 1 2
[2,] 4 8
[1] "double"
[1] 1 4 2 8
[1] "integer"
[1] "1" "4" "2" "8"
[1] "character"
[,1] [,2]
[1,] 1 3
[2,] 2 4
[,1] [,2]
[1,] 1 1
[2,] 1 1
[,1] [,2]
[1,] 2 4
[2,] 3 5
[,1] [,2]
[1,] 0 2
[2,] 1 3
[,1] [,2]
[1,] 10 30
[2,] 20 40
[,1] [,2]
[1,] 10 10
[2,] 10 10
Element by element matrix multiplication:
[,1] [,2]
[1,] 100 300
[2,] 200 400
Matrix multiplication:
[,1] [,2]
[1,] 400 400
[2,] 600 600
Divide matrix by a constant:
[,1] [,2]
[1,] 2 6
[2,] 4 8
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"
[1] "Apple Fruit" "Banana Fruit" "Cherry Fruit" "Date Fruit"
[1] "AppleFruit" "BananaFruit" "CherryFruit" "DateFruit"
[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."
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes