본문 바로가기

Contact English

【RStudio】 4강. 행렬

 

4강. 행렬(matrix)

 

추천글 : 【RStudio】 R 스튜디오 목차


1. 행렬 정의 [본문]

2. 행렬 입력 [본문]

3. 행렬 연산 [본문]


 

1. 행렬 정의 [목차]

 

A <- matrix(c(1, 4, 5, 2, 3, 4, 7, 8, 7), nrow = 3) 
A 
# [,1] [,2] [,3] 
# [1,] 1 2 7 
# [2,] 4 3 8 
# [3,] 5 4 7 
B <- data.frame(c(1, 4, 5), c(2, 3, 4), c(7, 8, 7)) 
B 
# c.1..4..5. c.2..3..4. c.7..8..7. 
# 1 1 2 7 
# 2 4 3 8 
# 3 5 4 7 
matrix(1:9, nrow = 3, byrow = TRUE) 
# [,1] [,2] [,3] 
# [1,] 1 2 3 
# [2,] 4 5 6 
# [3,] 7 8 9 
A[, 1] # the first column of A 
# [1] 1 4 5 
A[2, ] # the 2nd row of A 
# [1] 4 3 8 
A[, -3] # all the A except the 3rd column 
# [,1] [,2] 
# [1,] 1 2 
# [2,] 4 3 
# [3,] 5 4 
A[c(-1, -2), ] # all the A except the 1st and 2nd rows 
# [1] 5 4 7 
A[, c(1, 3)] 
# [,1] [,2] 
# [1,] 1 7 
# [2,] 4 8 
# [3,] 5 7 
b <- c(1, 3, 2)

 

 

2. 행렬 입력 [목차]

 

install.packages("ggplot2") 
library(ggplot2) 
help(diamonds) # description of diamonds 
# starting httpd help server ... done 

head(diamonds) # output the first 5 lines of diamonds 
# A tibble: 6 x 10 
# carat cut color clarity depth table price x y z 
# <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> 
# 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 
# 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 
# 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 
# 4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63 
# 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 
# 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 

tail(diamonds) # output the last 5 lines of diamonds 
# A tibble: 6 x 10 
# carat cut color clarity depth table price x y z 
# <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> 
# 1 0.72 Premium D SI1 62.7 59 2757 5.69 5.73 3.58 
# 2 0.72 Ideal D SI1 60.8 57 2757 5.75 5.76 3.5 
# 3 0.72 Good D SI1 63.1 55 2757 5.69 5.75 3.61 
# 4 0.7 Very Good D SI1 62.8 60 2757 5.66 5.68 3.56 
# 5 0.86 Premium H SI2 61 58 2757 6.15 6.12 3.74 
# 6 0.75 Ideal D SI2 62.2 55 2757 5.83 5.87 3.64 

summary(diamonds) 
# carat cut color clarity 
# Min. :0.2000 Fair : 1610 D: 6775 SI1 :13065 
# 1st Qu.:0.4000 Good : 4906 E: 9797 VS2 :12258 
# Median :0.7000 Very Good:12082 F: 9542 SI2 : 9194 
# Mean :0.7979 Premium :13791 G:11292 VS1 : 8171 
# 3rd Qu.:1.0400 Ideal :21551 H: 8304 VVS2 : 5066 
# Max. :5.0100 I: 5422 VVS1 : 3655 
# J: 2808 (Other): 2531 
# depth table price x 
# Min. :43.00 Min. :43.00 Min. : 326 Min. : 0.000 
# 1st Qu.:61.00 1st Qu.:56.00 1st Qu.: 950 1st Qu.: 4.710 
# Median :61.80 Median :57.00 Median : 2401 Median : 5.700 
# Mean :61.75 Mean :57.46 Mean : 3933 Mean : 5.731 
# 3rd Qu.:62.50 3rd Qu.:59.00 3rd Qu.: 5324 3rd Qu.: 6.540 
# Max. :79.00 Max. :95.00 Max. :18823 Max. :10.740 
# y z 
# Min. : 0.000 Min. : 0.000 
# 1st Qu.: 4.720 1st Qu.: 2.910
# Median : 5.710 Median : 3.530
# Mean : 5.735 Mean : 3.539 
# 3rd Qu.: 6.540 3rd Qu.: 4.040 
# Max. :58.900 Max. :31.800 

names(diamonds) 
# [1] "carat" "cut" "color" "clarity" "depth" "table" "price" 
# [8] "x" "y" "z" 
head(rownames(diamonds)) 
# [1] "1" "2" "3" "4" "5" "6" 
head(colnames(diamonds)) 
# [1] "carat" "cut" "color" "clarity" "depth" "table" 

# summary(price) makes an error 
attach(diamonds) 
head(summary(price)) 
# 605 802 625 828 776 698 
# 132 127 126 125 124 121 
detach(diamonds) 
# summary(price) makes an error 

table(color) 
# color 
# D E F G H I J 
# 6775 9797 9542 11292 8304 5422 2808 
table(color, cut) 
# cut 
# color Fair Good Very Good Premium Ideal 
# D 163 662 1513 1603 2834 
# E 224 933 2400 2337 3903 
# F 312 909 2164 2331 3826 
# G 314 871 2299 2924 4884 
# H 303 702 1824 2360 3115 
# I 175 522 1204 1428 2093 
# J 119 307 678 808 896 

ftable(color, cut) 
# cut Fair Good Very Good Premium Ideal
# color 
# D 163 662 1513 1603 2834
# E 224 933 2400 2337 3903 
# F 312 909 2164 2331 3826 
# G 314 871 2299 2924 4884 
# H 303 702 1824 2360 3115 
# I 175 522 1204 1428 2093 
# J 119 307 678 808 896 

table(color, cut, clarity) 
# , , clarity = I1 

# cut 
# color Fair Good Very Good Premium Ideal 
# D 4 8 5 12 13 
# E 9 23 22 30 18 
# F 35 19 13 34 42 
# G 53 19 16 46 16 
# H 52 14 12 46 38 
# I 34 9 8 24 17 
# J 23 4 8 13 2 

# , , clarity = SI2 

# cut 
# color Fair Good Very Good Premium Ideal 
# D 56 223 314 421 356 
# E 78 202 445 519 469 
# F 89 201 343 523 453 
# G 80 163 327 492 486 
# H 91 158 343 521 450 
# I 45 81 200 312 274 
# J 27 53 128 161 110 

# , , clarity = SI1 

# cut 
# color Fair Good Very Good Premium Ideal 
# D 58 237 494 556 738 
# E 65 355 626 614 766 
# F 83 273 559 608 608 
# G 69 207 474 566 660 
# H 75 235 547 655 763 
# I 30 165 358 367 504 
# J 28 88 182 209 243 

ftable(color, cut, clarity) 
# clarity I1 SI2 SI1 VS2 VS1 VVS2 VVS1 IF 
# color cut 
# D Fair 4 56 58 25 5 9 3 3 
# Good 8 223 237 104 43 25 13 9 
# Very Good 5 314 494 309 175 141 52 23 
# Premium 12 421 556 339 131 94 40 10 
# Ideal 13 356 738 920 351 284 144 28 
# E Fair 9 78 65 42 14 13 3 0 
# Good 23 202 355 160 89 52 43 9 
# Very Good 22 445 626 503 293 298 170 43 
# Premium 30 519 614 629 292 121 105 27 
# Ideal 18 469 766 1136 593 507 335 79 
# F Fair 35 89 83 53 33 10 5 4 
# Good 19 201 273 184 132 50 35 15 
# Very Good 13 343 559 466 293 249 174 67 
# Premium 34 523 608 619 290 146 80 31 
# Ideal 42 453 608 879 616 520 440 268 
# G Fair 53 80 69 45 45 17 3 2 
# Good 19 163 207 192 152 75 41 22 
# Very Good 16 327 474 479 432 302 190 79 
# Premium 46 492 566 721 566 275 171 87 
# Ideal 16 486 660 910 953 774 594 491 
# H Fair 52 91 75 41 32 11 1 0 
# Good 14 158 235 138 77 45 31 4 
# Very Good 12 343 547 376 257 145 115 29 
# Premium 46 521 655 532 336 118 112 40 
# Ideal 38 450 763 556 467 289 326 226 
# I Fair 34 45 30 32 25 8 1 0 
# Good 9 81 165 110 103 26 22 6 
# Very Good 8 200 358 274 205 71 69 19 
# Premium 24 312 367 315 221 82 84 23 
# Ideal 17 274 504 438 408 178 179 95 
# J Fair 23 27 28 23 16 1 1 0
# Good 4 53 88 90 52 13 1 6 
# Very Good 8 128 182 184 120 29 19 8 
# Premium 13 161 209 202 153 34 24 12 
# Ideal 2 110 243 232 201 54 29 25 

diamonds[order(carat), c(1, 2, 3) ] 
# A tibble: 53,940 x 3 
# carat cut color 
# <dbl> <ord> <ord> 
# 1 0.2 Premium E 
# 2 0.2 Premium E 
# 3 0.2 Premium E 
# 4 0.2 Premium E 
# 5 0.2 Premium E 
# 6 0.2 Ideal E 
# 7 0.2 Premium F 
# 8 0.2 Ideal D 
# 9 0.2 Very Good E 
# 10 0.2 Ideal E 
# ... with 53,930 more rows 

head(diamonds[["price"]]) 
# [1] 326 326 327 334 335 336 

head(diamonds$price) # vector format 
# [1] 326 326 327 334 335 336 

diamonds["price"]
# A tibble: 53,940 x 1 
# price 
# <int> 
# 1 326 
# 2 326 
# 3 327 
# 4 334 
# 5 335 
# 6 336 
# 7 336 
# 8 337 
# 9 337 
# 10 338 
# ... with 53,930 more rows 

diamonds[c(1, 3)] 
# A tibble: 53,940 x 2 
# carat color 
# <dbl> <ord> 
# 1 0.23 E 
# 2 0.21 E 
# 3 0.23 E 
# 4 0.290 I 
# 5 0.31 J 
# 6 0.24 J 
# 7 0.24 I 
# 8 0.26 H 
# 9 0.22 E 
# 10 0.23 H 
# ... with 53,930 more rows 

diamonds[c("price", "color")] 
# A tibble: 53,940 x 2 
# price color 
# <int> <ord> 
# 1 326 E 
# 2 326 E 
# 3 327 E 
# 4 334 I 
# 5 335 J 
# 6 336 J 
# 7 336 I 
# 8 337 H 
# 9 337 E 
# 10 338 H 
# ... with 53,930 more rows 

diamonds[carat < 0.7 & cut == "Premium", ] 
# A tibble: 5,443 x 10 
# carat cut color clarity depth table price x y z 
# <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> 
# 1 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 
# 2 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63 
# 3 0.22 Premium F SI1 60.4 61 342 3.88 3.84 2.33 
# 4 0.2 Premium E SI2 60.2 62 345 3.79 3.75 2.27 
# 5 0.32 Premium E I1 60.9 58 345 4.38 4.42 2.68 
# 6 0.24 Premium I VS1 62.5 57 355 3.97 3.94 2.47 
# 7 0.290 Premium F SI1 62.4 58 403 4.24 4.26 2.65 
# 8 0.22 Premium E VS2 61.6 58 404 3.93 3.89 2.41 
# 9 0.22 Premium D VS2 59.3 62 404 3.91 3.88 2.31 
# 10 0.3 Premium J SI2 59.3 61 405 4.43 4.38 2.61 
# ... with 5,433 more rows 

is.factor(color) 
# [1] TRUE 
is.numeric(color) 
# [1] FALSE 
is.numeric(price) 
# [1] TRUE 
price <- factor(price, levels = unique(price)) 
is.factor(price) 
# [1] TRUE 
class(color) 
# [1] "ordered" "factor" 
nlevels(color) # number of colors 
# [1] 7 
levels(color) # all the list of colors 
# [1] "D" "E" "F" "G" "H" "I" "J"

 

⑴ dim(x)

① x가 1차원 벡터인 경우 일차원 크기를 출력 

② x가 2차원 벡터인 경우 세로 크기(행의 개수), 가로 크기(열의 개수)의 이성분 벡터를 출력 (단, 첫 열, 첫 행은 무시)

⑵ rownames(TABLE) : TABLE에서 각각의 행을 나타내는 이름

⑶ matrix(data, nrow, ncol, ···)

data : 추가적인 데이터. 왠만하면 0이나 NA를 집어넣음

② nrow : 행의 개수

③ ncol : 열의 개수

⑷ substr(x, start, stop)

① x : a character vector

② start : the first element to be replaced

③ stop : the last element to be replaced

⑸ x[i,] (단, x는 TABLE)는 x의 i번째 행

⑹ x[,i] (단, x는 TABLE)는 x의 i번째 열

 

 

3. 행렬 연산 [목차]

 

# Error in library(matlib) : ‘matlib’이라고 불리는 패키지가 없습니다
A
# [,1] [,2] [,3]
# [1,] 1 2 7
# [2,] 4 3 8
# [3,] 5 4 7
A %*% A
# [,1] [,2] [,3]
# [1,] 44 36 72
# [2,] 56 49 108
# [3,] 56 50 116
solve(A, b)
# Ax = b → x = ???
# [1] 1.05 -1.60 0.45
det(A)
# [1] 20
inv(A)
# Error in inv(A) : 함수 "inv"를 찾을 수 없습니다 (라이브러리만 추가하면 됨)
is_symmetric_matrix(A) # Error in is_symmetric_matrix(A) :
# 함수 "is_symmetric_matrix"를 찾을 수 없습니다
t(A)
# [,1] [,2] [,3]
# [1,] 1 4 5
# [2,] 2 3 4
# [3,] 7 8 7
all.equal( A, A )
# [1] TRUE



mat <- as.matrix(cbind(c(1,2,3,4),c(1,2,3,4)))
mat
##      [,1] [,2]
## [1,]    1    1
## [2,]    2    2
## [3,]    3    3
## [4,]    4    4

## 2~4행 합산
rowsum(mat, c(1,2,2,2))
##   [,1] [,2]
## 1    1    1
## 2    9    9

## 해석 : rowname을 (1,2,2,2)로 보고 (1, 2)로 축약하는 명령어인 것

 

 

입력 : 2019.10.27 10:11

 

'▶ 자연과학 > ▷ RStudio' 카테고리의 다른 글

【RStudio】 6강. 그래프 그리기  (0) 2019.10.27
【RStudio】 5강. 데이터 입출력  (0) 2019.10.27
【RStudio】 3강. 배열  (0) 2019.09.23
【RStudio】 2강. R의 기초  (0) 2019.09.23
【RStudio】 1강. 개요  (0) 2019.09.06