빅데이터 분석과 R 프로그래밍 1: Ⅱ. 벡터, 행렬의 연산 및 함수

  • POSTECH에서 제공하는 MOOC 중, 빅데이터분석과 R프로그래밍 Ⅰ 과정입니다.

1. 벡터 및 행렬 생성

벡터 생성

  • 벡터
    • seq 함수 사용 (sequence)

0부터 10까지, 20개의 값을 생성

y1 <- seq(0, 10, length=20)
y1

[1] 0.0000000 0.5263158 1.0526316 1.5789474 2.1052632 2.6315789

[7] 3.1578947 3.6842105 4.2105263 4.7368421 5.2631579 5.7894737

[13] 6.3157895 6.8421053 7.3684211 7.8947368 8.4210526 8.9473684

[19] 9.4736842 10.0000000

0부터 10까지, 0.5씩 간격을 두고 값을 생성

y2 <- seq(0, 10, by = 0.5)
y2

[1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0

[16] 7.5 8.0 8.5 9.0 9.5 10.0

  • rep 함수 사용 (replication)

1부터 4까지 두 번을 반복하여 생성

z1 <- rep(1:4, 2)
z1

[1] 1 2 3 4 1 2 3 4

1부터 5까지 다섯 번을 반복하여 생성

z2 <- rep(1:2, 5)
z2

[1] 1 2 1 2 1 2 1 2 1 2

  • 백터 결합
    • cbind: column bind (열 기준으로 결합)
x <- c(1, 3, 5, 7, 9)
c1 <- c(2, 4, 6, 8, 10)
c2 <- cbind(x, c1)
c2

x c1

[1,] 1 2

[2,] 3 4

[3,] 5 6

[4,] 7 8

[5,] 9 10

  • rbind: row bind (행으로 결합)
c3 <- rbind(x, c1)
c3

[,1] [,2] [,3] [,4] [,5]

x 1 3 5 7 9

c1 2 4 6 8 10

  • 행렬의 생성
    • matrix 함수

tow row matrix with 1 to 10

m1 <- matrix(1:10, nrow=2) #number of row
m1

[,1] [,2] [,3] [,4] [,5]

[1,] 1 3 5 7 9

[2,] 2 4 6 8 10

three columns matrix with 1:6

m2 <- matrix(1:6, ncol=3)
m2

[,1] [,2] [,3]

[1,] 1 3 5

[2,] 2 4 6

matrix filled by rows, defalut: filled by columns

m3 <- matrix(1:6, nrow=2, byrow=T)
m3

[,1] [,2] [,3]

[1,] 1 2 3

[2,] 4 5 6

  • 고차원 행렬_array 함수

higher order of array

a1 <- array(c(1:18), dim=c(3,3,2)) # 3 by 3 행렬을 2개 만듦
a1

, , 1

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

, , 2

[,1] [,2] [,3]

[1,] 10 13 16

[2,] 11 14 17

[3,] 12 15 18

2. 벡터 생성과 이름 주기

===========================

벡터 생성 및 벡터 이름 주기

  • (0,1) 값을 갖는 벡터 gender에 0=female, 1=male 값 부여
gender <- c(0,1)
names(gender) <- c("female", "male")
gender

female male

0 1

  • factor 변수로 정의
    • gender 변수에 (0,1)입력된 경우 -> gender를 factor 변수로 정의 필요
      gender 변수는 factor 변수로 인식하지 못함: (0,1)로 입력되었으므로

define as a factor variable

is.factor(gender)

[1] FALSE

  • as.factor(변수이름): 어떤 변수를 factor 변수로 정의할 때, gender 변수는 factor 변수로 정의 -> is.factor(gender)로 확인하면 factor 변수로 정의된 것 확인 가능
gender <- as.factor(gender)
is.factor(gender)

[1] TRUE

  • 범주형 변수 생성_factor 사용
    • size라는 변수 생성: (S, M, L, XL) 값을 갖는 범주형 변수(factor) 생성
size <- c("S", "M", "L", "XL")

define size as a factor (categorical variable)

size_factor <- factor(size)

size_factor

[1] S M L XL

Levels: L M S XL

  • 순서를 정의한 factor 생성
size_factor1 <- factor(size, ordered = TRUE, levels = c("S", "M", "L", "XL"))
size_factor

[1] S M L XL

Levels: L M S XL

  • 행렬 생성하고 차원 알아보기
x <- matrix(rnorm(12), nrow=4)
x

[,1] [,2] [,3]

[1,] -0.003729970 1.4663220 0.04381181

[2,] 1.147397993 1.2467755 -0.48528371

[3,] -0.002165277 -0.4026005 -1.08181700

[4,] -0.972551871 0.5998451 1.70662398

차원 확인하기

dim(x)

[1] 4 3

  • 행렬의 속성
    • 행렬 x는 (4*3), x가 data frame은 아님

generate matrix form normal

x <- matrix(rnorm(12), nrow=4)
x

[,1] [,2] [,3]

[1,] -0.71917921 0.1306288 1.3220113

[2,] 0.67408016 -0.3067689 -0.5009565

[3,] 0.59011810 0.4158816 0.5064173

[4,] 0.03118919 -0.7860212 -0.6759638

check dimension of x

dim(x)

[1] 4 3

data frame

is.data.frame(x)

[1] FALSE

matrix x is not a data frame

  • as.data.frame(x)는 x를 데이터로 인식

defime x as a data frame

x <- as.data.frame(x)

then x is a data frame

is.data.frame(x)

[1] TRUE

3. 백터와 행렬의 연산

====================

  • 기본 연산 기호
2^3

[1] 8

4**3

[1] 64

7%%2

[1] 1

7%/%2

[1] 3

  • 행렬의 연산
    - 참고

    • 전치행렬(transpose) 구하기 (t)
      전치행렬은 행과 열을 바꾼 행렬

      m2는 (2 * 3)행렬, tm2는 (3 * 2) 행렬

m2 <- matrix(1:6, ncol=3)
m2

[,1] [,2] [,3]

[1,] 1 3 5

[2,] 2 4 6

transpose of m2

tm2 <- t(m2)
tm2

[,1] [,2]

[1,] 1 2

[2,] 3 4

[3,] 5 6

  • determinant(행렬식) 구하기 (det)
    determinant 식 |A| = $\frac{|a b|}{[c d]}$ = ad = bc
d1 <- matrix(1:4, nrow=2, byrow=T) # row부터 채워라
d1

[,1] [,2]

[1,] 1 2

[2,] 3 4

det(d1)

[1] -2

  • 역행렬(inverse) 구하기 (solve)
    d1 = $\frac{[1 2]}{3 4]}$,

    inverse(d1) = fra**c[ − 2.01.0][1.5 − 0.5]

inverse of matrix

d1_inv <- solve(d1)
d1_inv

[,1] [,2]

[1,] -2.0 1.0

[2,] 1.5 -0.5

d1 * inv(dq) = identity matrix

d1 %*% d1_inv

[,1] [,2]

[1,] 1 1.110223e-16

[2,] 0 1.000000e+00

: d1*d1의 역행렬 = 단위행렬(대각행렬이 1인 행렬)

  • 역행렬을 이용한 방정식 해 구하기 (solve)
    3x + 2y = 8, x + y = 2

    방정식의 해를 구하기 위해 a(행렬)과 b(벡터)를 생성

solve equation

3x + 2y = 8, x + y =2

matrix a, b

a <- matrix(c(3,1,2,1), nrow=2, ncol=2)
b <- matrix(c(8,2), nrow=2, ncol=1)
a

[,1] [,2]

[1,] 3 2

[2,] 1 1

b

[,1]

[1,] 8

[2,] 2

: solve 함수를 이용해 x와 y의 해를 찾음

solve(a,b)

[,1]

[1,] 4

[2,] -2

  • 고유치(eigenvalue)와 고유벡터(eigenvector)

example for eigen value and eigen vector

already centered matrix

x <- matrix(c(-3, -2, 0, 1, 2, 2, -3, -3, 0, 2, 2, 2, 5, 7, 4, 0, -5, -11), nrow=6, ncol=3)
x

[,1] [,2] [,3]

[1,] -3 -3 5

[2,] -2 -3 7

[3,] 0 0 4

[4,] 1 2 0

[5,] 2 2 -5

[6,] 2 2 -11

dim(x)

[1] 6 3

eigen value and eigen vector

e1 <- eigen(t(x)%*%x)
e1

eigen() decomposition

$values

[1] 273.546962 13.845220 0.607818

$vectors

[,1] [,2] [,3]

[1,] -0.2525343 0.5487321 0.79694382

[2,] -0.2841664 0.7452586 -0.60319073

[3,] 0.9249194 0.3787911 0.03227211

4. 간단한 함수생성 및 루프(for, while)

================

함수 생성

  • squre 함수 생성

create a simple function

squre function

squre <- function(x){
  return(x*x) # 제곱값 제공
}

squre(9)

[1] 81

squre(1:3) #1부터 3까지 각 squre 값 제공하라

[1] 1 4 9

  • dif 함수 생성
dif <- function(x,y){
  return(x-y)
}

dif(20,10)

[1] 10

  • rootdif 함수 생성
rootdif <- function(x,y){
  return(sqrt(x-y))
}

rootdif(20,10)

[1] 3.162278

  • 기존 함수의 코드 보기_round 함수

round 함수: 소수점 자리 조정하는 함수

round(5.14846)

[1] 5

round(5.14846, 2)

[1] 5.15

to see the function “round”

round

function (x, digits = 0) .Primitive(“round”)

plus

round(rootdif(20,10))

[1] 3

round(rootdif(20,10),2)

[1] 3.16

루프문

  • 루프문 (for)
    • for을 사용한 루프 1

for 1 to 10

if remainder = 1 when deviding by 3

then go to next number

$$

for(i in 1:10){
  if(i%%3 == 1){
    next()
  }
  print(i)
}

[1] 2

[1] 3

[1] 5

[1] 6

[1] 8

[1] 9

1부터 10까지의 숫자 반복

3으로 나누었을 때 내머지가 1인 경우 next(다음 숫자로 넘어감)

넘어가지 않은 경우 print(i)를 통해 결과 반환

  • for을 사용한 루프 2

i = 1 ~ 10 1, 2, 3 … 더해가며 프린트하는데, i > 5 보다 크면 수행(print(i))을 멈춤

for loop example 2

stop loop after i > 5

%%

for (i in 1:10){
  i <- i + 1
  print(i)
  if (i > 5){
  # stop loop after i > 5
    break
  }
}

[1] 2

[1] 3

[1] 4

[1] 5

[1] 6

  • while을 사용한 루프

y가 5보다 적을 때는 {expression} 부분 수행

while loop

while (condition) {expression}

y = 0
while(y < 5){print(y <- y+1)}

[1] 1

[1] 2

[1] 3

[1] 4

[1] 5

Share