Matrizes
Criação
Criando uma matriz com 3 linhas e 4 colunas:
v1 <- 1:12
v1
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
m1 <- matrix(v1, nrow = 3)
m1
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
m2 <- matrix(v1, nrow = 3, byrow = T)
m2
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
Classe e tipo
class(m2)
## [1] "matrix" "array"
typeof(m2)
## [1] "integer"
Teste
is.matrix(m2)
## [1] TRUE
is.integer(m2)
## [1] TRUE
Conversão
De matriz do tipo integer para matriz do tipo character:
m2 <- matrix(as.character(m2), nrow = 3)
m2
## [,1] [,2] [,3] [,4]
## [1,] "1" "2" "3" "4"
## [2,] "5" "6" "7" "8"
## [3,] "9" "10" "11" "12"
typeof(m2)
## [1] "character"
De matriz do tipo character para matriz do tipo numeric:
m2 <- matrix(as.numeric(m2), nrow = 3)
m2
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
typeof(m2)
## [1] "double"
De matriz para um vetor numérico:
v2 <- as.numeric(m2)
v2
## [1] 1 5 9 2 6 10 3 7 11 4 8 12
v2 <- sort(v2)
v2
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
class(v2)
## [1] "numeric"
De matriz para data frame:
df <- as.data.frame(v2)
df
## v2
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
## 6 6
## 7 7
## 8 8
## 9 9
## 10 10
## 11 11
## 12 12
class(df)
## [1] "data.frame"
Nomes
paciente1 <- c(1.33, 4.02, 5.24, 2.32)
paciente2 <- c(1.44, 2.76, 6.08, 3.13)
m_pacientes <- matrix(c(paciente1, paciente2), nrow=2, byrow = T)
m_pacientes
## [,1] [,2] [,3] [,4]
## [1,] 1.33 4.02 5.24 2.32
## [2,] 1.44 2.76 6.08 3.13
Colunas
colnames(m_pacientes) <- c("exame_a", "exame_b", "exame_c", "exame_d")
m_pacientes
## exame_a exame_b exame_c exame_d
## [1,] 1.33 4.02 5.24 2.32
## [2,] 1.44 2.76 6.08 3.13
Linhas
rownames(m_pacientes) <- c("paciente1", "paciente2")
m_pacientes
## exame_a exame_b exame_c exame_d
## paciente1 1.33 4.02 5.24 2.32
## paciente2 1.44 2.76 6.08 3.13
Matemática
Operações básicas
A <- matrix(1:4, nrow=2, byrow = T)
A
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
B <- matrix(2:5, nrow=2, byrow = T)
B
## [,1] [,2]
## [1,] 2 3
## [2,] 4 5
C <- matrix(3:8, nrow=2, byrow = T)
C
## [,1] [,2] [,3]
## [1,] 3 4 5
## [2,] 6 7 8
D <- matrix(3:8, nrow=3, byrow = T)
D
## [,1] [,2]
## [1,] 3 4
## [2,] 5 6
## [3,] 7 8
Soma
A + 10
## [,1] [,2]
## [1,] 11 12
## [2,] 13 14
A + B
## [,1] [,2]
## [1,] 3 5
## [2,] 7 9
A + C
## Error in A + C: non-conformable arrays
A + D
## Error in A + D: non-conformable arrays
Multiplicação
A * 10
## [,1] [,2]
## [1,] 10 20
## [2,] 30 40
A * B
## [,1] [,2]
## [1,] 2 6
## [2,] 12 20
A * C
## Error in A * C: non-conformable arrays
A * D
## Error in A * D: non-conformable arrays
Álgebra Linear
Multiplicação entre matrizes:
A %*% B
## [,1] [,2]
## [1,] 10 13
## [2,] 22 29
B %*% A
## [,1] [,2]
## [1,] 11 16
## [2,] 19 28
A %*% C
## [,1] [,2] [,3]
## [1,] 15 18 21
## [2,] 33 40 47
C %*% A
## Error in C %*% A: non-conformable arguments
Transposta:
t(A)
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
Inversa:
solve(A)
## [,1] [,2]
## [1,] -2.0 1.0
## [2,] 1.5 -0.5
Determinante:
det(A)
## [1] -2
Operações relacionais
A <- matrix(c(1,2,3,4), nrow=2)
A
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
B <- matrix(c(1,3,2,4), nrow=2)
B
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
A > B
## [,1] [,2]
## [1,] FALSE TRUE
## [2,] FALSE FALSE
A < B
## [,1] [,2]
## [1,] FALSE FALSE
## [2,] TRUE FALSE
A >= B
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] FALSE TRUE
A <= B
## [,1] [,2]
## [1,] TRUE FALSE
## [2,] TRUE TRUE
A == B
## [,1] [,2]
## [1,] TRUE FALSE
## [2,] FALSE TRUE
A != B
## [,1] [,2]
## [1,] FALSE TRUE
## [2,] TRUE FALSE
Outras operações
colSums(m_pacientes)
## exame_a exame_b exame_c exame_d
## 2.77 6.78 11.32 5.45
rowSums(m_pacientes)
## paciente1 paciente2
## 12.91 13.41
colMeans(m_pacientes)
## exame_a exame_b exame_c exame_d
## 1.385 3.390 5.660 2.725
rowMeans(m_pacientes)
## paciente1 paciente2
## 3.2275 3.3525
Indexação
Selecionando a segunda coluna:
C[,2]
## [1] 4 7
class(C[,2])
## [1] "integer"
Selecionando a primeira linha:
C[1,]
## [1] 3 4 5
class(C[1,])
## [1] "integer"
Selecionando a célula da primeira linha e segunda coluna:
C[1,2]
## [1] 4
class(C[1,2])
## [1] "integer"