A matrix is a two dimensional data structure that is similar to a vector but additionally contains the dimension attribute.
When creating a matrix, the last two arguments to matrix tell it the number of rows and columns the matrix should have.
You can use the byrow=TRUE argument to create a matrix by rows instead of by columns as shown below:
mymat <- matrix(1:12,4,3)
mymat
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
mymat <- matrix(1:12,ncol=3,byrow=TRUE)
mymat
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
kenya <- c(460.998, 314.4)
ethiopia <- c(290.475, 247.900)
chad <- c(309.306, 165.8)
geography_matrix <- matrix(c(kenya, ethiopia, chad), nrow = 3, byrow = TRUE)
location <- c("Lat", "Long")
countries <- c("Kenya", "Ethiopia", "Chad")
colnames(geography_matrix) <- location
rownames(geography_matrix) <- countries
geography_matrix
## Lat Long
## Kenya 460.998 314.4
## Ethiopia 290.475 247.9
## Chad 309.306 165.8
Matriachy <- c("Matriachy", 60, "F", "Queen")
Ryan <- c("Ryan", 7, "M", "Student")
Dynasty <- c("Dynasty", 30, "F", "Scientist")
Migan <- c("Migan", 26, "F", "Chef")
Wolfcode <- c("Wolfcode", 19, "M", "Engineer")
family <- matrix(c(Matriachy, Ryan, Dynasty, Migan, Wolfcode), nrow = 5, byrow = TRUE)
Header <- c("Name", "Age", "Gender", "Occupation")
Name <- c("Matriachy", "Ryan", "Dynasty", "Migan", "Wolfcode")
colnames(family) <- Header
rownames(family) <- Name
family
## Name Age Gender Occupation
## Matriachy "Matriachy" "60" "F" "Queen"
## Ryan "Ryan" "7" "M" "Student"
## Dynasty "Dynasty" "30" "F" "Scientist"
## Migan "Migan" "26" "F" "Chef"
## Wolfcode "Wolfcode" "19" "M" "Engineer"
You can add a row to a matrix using the rbind() function.
x <- matrix(1:9, nrow = 3)
cbind(x, c(1, 2, 3))
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 1
## [2,] 2 5 8 2
## [3,] 3 6 9 3
cbind(family, c("Arabia Terra", "Amazonis Planitia", "Amazonis Planitia", "Arabia Terra", "Amazonis Planitia"))
## Name Age Gender Occupation
## Matriachy "Matriachy" "60" "F" "Queen" "Arabia Terra"
## Ryan "Ryan" "7" "M" "Student" "Amazonis Planitia"
## Dynasty "Dynasty" "30" "F" "Scientist" "Amazonis Planitia"
## Migan "Migan" "26" "F" "Chef" "Arabia Terra"
## Wolfcode "Wolfcode" "19" "M" "Engineer" "Amazonis Planitia"
x <- matrix(1:9, nrow = 3)
rbind(x,c(1,2,3))
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
## [4,] 1 2 3
rbind(family, c("Ruby", 3, "F", "Youngling", "Amazonis Planitia"))
## Warning in rbind(family, c("Ruby", 3, "F", "Youngling", "Amazonis Planitia")):
## number of columns of result is not a multiple of vector length (arg 2)
## Name Age Gender Occupation
## Matriachy "Matriachy" "60" "F" "Queen"
## Ryan "Ryan" "7" "M" "Student"
## Dynasty "Dynasty" "30" "F" "Scientist"
## Migan "Migan" "26" "F" "Chef"
## Wolfcode "Wolfcode" "19" "M" "Engineer"
## "Ruby" "3" "F" "Youngling"
To select an element of a matrix, one needs to specify both the row and the column as shown:
x <- matrix(1:9, nrow = 3)
x[1,3]
## [1] 7
x[2, ]
## [1] 2 5 8
x[ ,3]
## [1] 7 8 9
family[5, ]
## Name Age Gender Occupation
## "Wolfcode" "19" "M" "Engineer"
family[1, ]
## Name Age Gender Occupation
## "Matriachy" "60" "F" "Queen"
Matrix addition and subtract require the matrices to have the same dimensions.
x <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
y <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
x
## [,1] [,2] [,3]
## [1,] 3 -1 2
## [2,] 9 4 6
y
## [,1] [,2] [,3]
## [1,] 5 0 3
## [2,] 2 9 4
x + y
## [,1] [,2] [,3]
## [1,] 8 -1 5
## [2,] 11 13 10
x - y
## [,1] [,2] [,3]
## [1,] -2 -1 -1
## [2,] 7 -5 2
Let’s create two 2 x 3 matrices x and y
x <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
y <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
z <- x * y
z
## [,1] [,2] [,3]
## [1,] 15 0 6
## [2,] 18 36 24
z <- x/y
z
## [,1] [,2] [,3]
## [1,] 0.6 -Inf 0.6666667
## [2,] 4.5 0.4444444 1.5000000