[14-01-22]

5.2 행렬내 데이터 접근

x <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), ncol = 3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
x[1, 1]
## [1] 1
x[1, 2]
## [1] 4
x[2, 2]
## [1] 5
x[1:2, ]
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
x[-3, ]
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
x[c(1, 3), c(1, 3)]
##      [,1] [,2]
## [1,]    1    7
## [2,]    3    9
x <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), ncol = 3, dimnames = list(c("item1", 
    "item2", "item3"), c("feature1", "feature2", "feature3")))
x["item1", c("feature1", "feature3")]
## feature1 feature3 
##        1        7

5.3 행렬의 연산

x * 2
##       feature1 feature2 feature3
## item1        2        8       14
## item2        4       10       16
## item3        6       12       18
x/2
##       feature1 feature2 feature3
## item1      0.5      2.0      3.5
## item2      1.0      2.5      4.0
## item3      1.5      3.0      4.5
x + x
##       feature1 feature2 feature3
## item1        2        8       14
## item2        4       10       16
## item3        6       12       18
x - x
##       feature1 feature2 feature3
## item1        0        0        0
## item2        0        0        0
## item3        0        0        0
x %*% x
##       feature1 feature2 feature3
## item1       30       66      102
## item2       36       81      126
## item3       42       96      150
x <- matrix(c(1, 2, 3, 4), ncol = 2)
solve(x)
##      [,1] [,2]
## [1,]   -2  1.5
## [2,]    1 -0.5
x %*% solve(x)
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
x <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), ncol = 3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
t(x)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
ncol(x)
## [1] 3
nrow(x)
## [1] 3

6. 배열

6.1 배열 정의

matrix(1:12, ncol = 4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
array(1:12, dim = c(3, 4))
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
array(1:12, dim = c(2, 2, 3))
## , , 1
## 
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    5    7
## [2,]    6    8
## 
## , , 3
## 
##      [,1] [,2]
## [1,]    9   11
## [2,]   10   12

6.2 배열 데이터 접근

x <- array(1:12, dim = c(2, 2, 3))
x[1, 1, 1]
## [1] 1
x[1, 2, 3]
## [1] 11
x[, , 3]
##      [,1] [,2]
## [1,]    9   11
## [2,]   10   12
dim(x)
## [1] 2 2 3

7. 데이터 프레임(Data Frame)

7.1 데이터 프레임 정의

d <- data.frame(x = c(1, 2, 3, 4, 5), y = c(2, 4, 6, 8, 10))
d
##   x  y
## 1 1  2
## 2 2  4
## 3 3  6
## 4 4  8
## 5 5 10

데이터 타입을 혼용해서 사용할 수 있다.

d <- data.frame(x = c(1, 2, 3, 4, 5), y = c(2, 4, 6, 8, 10), z = c("M", "F", 
    "M", "F", "M"))
d
##   x  y z
## 1 1  2 M
## 2 2  4 F
## 3 3  6 M
## 4 4  8 F
## 5 5 10 M

이미 정의된 데이터 프레임에 새열을 추가하고자 한다면

d$v <- c(3, 6, 9, 12, 15)
d
##   x  y z  v
## 1 1  2 M  3
## 2 2  4 F  6
## 3 3  6 M  9
## 4 4  8 F 12
## 5 5 10 M 15