INTRODUCTION TO R
Create and Name
Matrices
Introduction to R
Matrix
Vector: 1D array of data elements
Matrix: 2D array of data elements
Rows and columns
One atomic vector type
Introduction to R
Create a matrix
matrix()
> matrix(1:6, nrow = 2)
[,1] [,2] [,3]
[1,]
1
3
5
[2,]
2
4
6
> matrix(1:6, ncol = 3)
[,1] [,2] [,3]
[1,]
1
3
5
[2,]
2
4
6
> matrix(1:6, nrow = 2, byrow = TRUE)
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6
Introduction to R
Create a matrix: recycling
> matrix(1:3, nrow = 2, ncol = 3)
[,1] [,2] [,3]
[1,]
1
3
2
[2,]
2
1
3
> matrix(1:4, nrow = 2, ncol = 3)
[,1] [,2] [,3]
[1,]
1
3
1
[2,]
2
4
2
Warning message:
In matrix(1:4, nrow = 2, ncol = 3) :
data length [4] is not a sub-multiple or multiple of the
number of columns [3]
Introduction to R
rbind(), cbind()
> cbind(1:3, 1:3)
[,1] [,2]
[1,]
1
1
[2,]
2
2
[3,]
3
3
> rbind(1:3, 1:3)
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
1
2
3
Introduction to R
rbind(), cbind()
> m <- matrix(1:6, byrow = TRUE, nrow = 2)
> rbind(m, 7:9)
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6
[3,]
7
8
9
> cbind(m, c(10, 11))
[,1] [,2] [,3] [,4]
[1,]
1
2
3
10
[2,]
4
5
6
11
Introduction to R
Naming a matrix rownames(), colnames()
> m <- matrix(1:6, byrow = TRUE, nrow = 2)
> rownames(m) <- c("row1", "row2")
> m
[,1] [,2] [,3]
row1
1
2
3
row2
4
5
6
> colnames(m) <- c("col1", "col2", "col3")
> m
col1 col2 col3
row1
1
2
3
row2
4
5
6
Introduction to R
Naming a matrix
> m <- matrix(1:6, byrow = TRUE, nrow = 2)
Introduction to R
Naming a matrix
> m <- matrix(1:6, byrow = TRUE, nrow = 2,
dimnames = list(c("row1", "row2"),
c("col1", "col2", "col3")))
> m
col1 col2 col3
row1
1
2
3
row2
4
5
6
Introduction to R
Coercion
> num <- matrix(1:8, ncol = 2)
> num
[,1] [,2]
[1,]
1
5
[2,]
2
6
[3,]
3
7
[4,]
4
8
> char <- matrix(LETTERS[1:6], nrow = 4, ncol = 3)
> char
[,1] [,2] [,3]
[1,] "A" "E" "C"
[2,] "B" "F" "D"
[3,] "C" "A" "E"
[4,] "D" "B" "F"
Introduction to R
Coercion
> num <- matrix(1:8, ncol = 2)
> char <- matrix(LETTERS[1:6], nrow = 4, ncol = 3)
> cbind(num, char)
[,1] [,2] [,3]
[1,] "1" "5" "A"
[2,] "2" "6" "B"
[3,] "3" "7" "C"
[4,] "4" "8" "D"
[,4]
"E"
"F"
"A"
"B"
[,5]
"C"
"D"
"E"
"F"
Contain dierent types? list or data.frame
INTRODUCTION TO R
Lets practice!