1. feladat. Mátrix létrehozása.
Hozzunk létre egy 3 sorból és 4 oszlopból álló mátrixot! Nevezzük el a sorokat, az oszlopkat és az egyes dimenziókat! Transzponáljuk a létrehozott mátrixot!
dim()
függvénnyelm.1 <- 1:12
dim(m.1) <- c(3,4)
m.1; typeof(m.1); length(m.1); attributes(m.1)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[1] "integer"
[1] 12
$dim
[1] 3 4
matrix()
függvénnyelm.1 <- matrix(1:12, nrow = 3, ncol = 4, byrow = T)
m.1; typeof(m.1); length(m.1); attributes(m.1)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[1] "integer"
[1] 12
$dim
[1] 3 4
rbind()
függvénnyelm.1 <- rbind(1:4, 1:4, 1:4)
m.1; typeof(m.1); length(m.1); attributes(m.1)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
[1] "integer"
[1] 12
$dim
[1] 3 4
cbind()
függvénnyelm.1 <- cbind(1:3, 4:6, 7:9, 10:12)
m.1; typeof(m.1); length(m.1); attributes(m.1)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[1] "integer"
[1] 12
$dim
[1] 3 4
rownames(m.1) <- c("a", "b", "c") # sorok elnevezése
colnames(m.1) <- c("A", "B", "C", "D") # oszlopok elnevezése
names(dimnames(m.1)) <- c("1. dim", "2. dim") # dimenziók elnevezése
m.1; typeof(m.1); length(m.1); attributes(m.1)
2. dim
1. dim A B C D
a 1 4 7 10
b 2 5 8 11
c 3 6 9 12
[1] "integer"
[1] 12
$dim
[1] 3 4
$dimnames
$dimnames$`1. dim`
[1] "a" "b" "c"
$dimnames$`2. dim`
[1] "A" "B" "C" "D"
t(m.1)
1. dim
2. dim a b c
A 1 2 3
B 4 5 6
C 7 8 9
D 10 11 12