letters
letters[22]
letters[1:7]
letters
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
## [20] "t" "u" "v" "w" "x" "y" "z"
letters[22]
## [1] "v"
letters[1:7]
## [1] "a" "b" "c" "d" "e" "f" "g"
mtx <- matrix(1:12,nrow=3,ncol=4)
rownames(mtx) <- letters[1:3]
colnames(mtx) <- letters[4:7]
# list 활용:
# dimnames(mtx) <- list(letters[1:3], letters[1:4])
mtx
## d e f g
## a 1 4 7 10
## b 2 5 8 11
## c 3 6 9 12
mtx1 <- mtx[,c(1,3)]
mtx2 <- mtx[,c(2,4)]
mtx.c <- cbind(mtx1,mtx2)
# mtx.c <- cbind(mtx[,c(1,3)], mtx[,c(2,4)])
mtx.c
## d f e g
## a 1 7 4 10
## b 2 8 5 11
## c 3 9 6 12
3 * mtx
3*mtx
## d e f g
## a 3 12 21 30
## b 6 15 24 33
## c 9 18 27 36
mtx + 3
mtx+3
## d e f g
## a 4 7 10 13
## b 5 8 11 14
## c 6 9 12 15
mtx + mtx.c
mtx + mtx.c
## d e f g
## a 2 11 11 20
## b 4 13 13 22
## c 6 15 15 24
mtx - mtx.c
mtx-mtx.c
## d e f g
## a 0 -3 3 0
## b 0 -3 3 0
## c 0 -3 3 0
mtx * mtx.c
mtx*mtx.c
## d e f g
## a 1 28 28 100
## b 4 40 40 121
## c 9 54 54 144
mtx / mtx.c
mtx / mtx.c
## d e f g
## a 1 0.5714286 1.75 1
## b 1 0.6250000 1.60 1
## c 1 0.6666667 1.50 1
mtx %*% mtx.c
# Error in mtx %*% mtx.c : 적합한 인자들이 아닙니다
# mtx(3x4)와 mtx.c(3x4)에서, 4와 3이 다름으로 계산 불가능!
matrix(1:4, nrow=2)^(1:4)
matrix(1:4, nrow=2)^(1:4)
## [,1] [,2]
## [1,] 1 27
## [2,] 4 256
x1 <- 1:10
x2 <- x1*2
x3 <- x1*3
x4 <- x1*4
x5 <- x1*5
mtx <- cbind(x1,x2,x3,x4,x5)
# x <- 1:10
# mtx <- cbind(x1=x, x2=x*2, x3=x*3, x4=x*4, x5=x*5)
# mtx <- cbind(x, x*2, x*3, x*4, x*5) 단, colnames(mtx)는 "x" "" "" "" ""
rownames(mtx) <- letters[1:10]
mtx
## x1 x2 x3 x4 x5
## a 1 2 3 4 5
## b 2 4 6 8 10
## c 3 6 9 12 15
## d 4 8 12 16 20
## e 5 10 15 20 25
## f 6 12 18 24 30
## g 7 14 21 28 35
## h 8 16 24 32 40
## i 9 18 27 36 45
## j 10 20 30 40 50
sum(mtx['h',])
## [1] 120
mtx <- matrix(1:9999, ncol=9)
mtx.sub <- mtx[(dim(mtx)[1]-2):dim(mtx)[1],(dim(mtx)[2]-1):dim(mtx)[2]]
mtx.sub
## [,1] [,2]
## [1,] 8886 9997
## [2,] 8887 9998
## [3,] 8888 9999
x <- c(1:3)
y <- x^2
z <- letters[1:3]
x <- c(1:3)
x
## [1] 1 2 3
class(x) # 정수형
## [1] "integer"
y <- x^2
y
## [1] 1 4 9
class(y) # 실수형
## [1] "numeric"
z <- letters[1:3]
z
## [1] "a" "b" "c"
class(z) # 문자형
## [1] "character"
mtx <- cbind(x,y,z)
class(mtx[,1]) # x
## [1] "character"
class(mtx[,2]) # y
## [1] "character"
class(mtx[,3]) # z
## [1] "character"
mtx
## x y z
## [1,] "1" "1" "a"
## [2,] "2" "4" "b"
## [3,] "3" "9" "c"
mtx <- matrix(1:12,nrow=3,ncol=4)
mtx
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
mtx * 1
mtx * 1
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
mtx %*% matrix(1, ncol(mtx)) : mtx와 다름!!!
mtx %*% matrix(1,ncol(mtx))
## [,1]
## [1,] 22
## [2,] 26
## [3,] 30
t(t(mtx))
t(t(mtx))
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
mtx %*% diag(ncol(mtx))
mtx %*% diag(ncol(mtx))
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
diag(3) # 대각행렬: 정방행렬 중에 대각요소를 제외한 원소들이 0인 것
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
matrix(1:10000, 1000)
mtx <- matrix(1:10000, 1000)
mtx[777,3]
## [1] 2777
a <- 1:24
dim(a) <- c(3,4,2)
# a <- array(1:24, dim=c(3,4,2))
a
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 13 16 19 22
## [2,] 14 17 20 23
## [3,] 15 18 21 24