lec. 2 : introduction to r part 2statistics.ssu.ac.kr/~cc/sit/courses/2017summer... ·...

24
Soongsil University Lec. 2 : Introduction to R – Part 2 Big Data Analytics Short Course 17. 07. 04

Upload: others

Post on 16-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

Lec. 2 : Introduction to R – Part 2

Big Data Analytics Short Course

17. 07. 04

Page 2: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• factor() : factor 생성하기

• 자료의 class가 바뀌는 것을 확인

R의 데이터 구조 : Factor

> region = c("A","A","B","C","D") > region [1] "A" "A" "B" "C" "D"

> class(region) [1] "character"

> region.fac = factor(region) > region.fac [1] A A B C D Levels: A B C D

> class(region.fac) [1] "factor"

Page 3: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• levels()

• factor의 구성을 바꾸고 싶을 때 사용

R의 데이터 구조 : Factor

> region.fac [1] A A B C D Levels: A B C D

> levels(region.fac) = c(1,2,3,4) > region.fac [1] 1 1 2 3 4 Levels: 1 2 3 4

Page 4: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• cut() : 연속형 자료를 범주화 할 때 사용, ex) 나이, 온도, ...

• 이 자료의 범주를 바꾸기

R의 데이터 구조 : Factor

> a = 1:8 > a [1] 1 2 3 4 5 6 7 8

> cut(a, breaks=c(0,3,6,10)) [1] (0,3] (0,3] (0,3] (3,6] (3,6] (3,6] (6,10] (6,10] Levels: (0,3] (3,6] (6,10]

> x = cut(a, breaks=c(0,3,6,10))

> x [1] Low Low Low Mid Mid Mid High High Levels: Low Mid High

Page 5: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• ordered() : 범주형 자료에 순서를 부여

• 이 경우에는 Low, Mid, High에 순서가 없다.

• ordered()로 범주에 순서를 부여

R의 데이터 구조 : Factor

> x [1] Low Low Low Mid Mid Mid High High Levels: Low Mid High

> ordered(x) [1] Low Low Low Mid Mid Mid High High Levels: Low < Mid < High

> ordered(x, levels=c("High", "Mid", "Low")) [1] Low Low Low Mid Mid Mid High High Levels: High < Mid < Low

Page 6: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• matrix() : matrix 생성

• length()?

R의 데이터 구조 : Matrix

> matrix(1:6, nrow=2, ncol=3)[,1] [,2] [,3]

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

> matrix(1:6, nrow=2, ncol=3, byrow=T) [,1] [,2] [,3]

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

> a = matrix(1:6, nrow=2, ncol=3) > length(a) [1] 6

Page 7: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• dim() : 행렬의 차원

• t() : 행렬을 전치

R의 데이터 구조 : Matrix

> a [,1] [,2] [,3]

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

> dim(a) [1] 2 3

> t(a) [,1] [,2]

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

Page 8: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• 단일 숫자의 덧셈

• 벡터의 덧셈

• 배수가 다른 벡터의 덧셈

R의 데이터 구조 : Matrix의 계산

> a + 1 [,1] [,2] [,3]

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

> a + c(10,100) [,1] [,2] [,3]

[1,] 11 13 15 [2,] 102 104 106

> a + c(1,10,30,50) [,1] [,2] [,3]

[1,] 2 33 6 [2,] 12 54 16 Warning message: In a + c(1, 10, 30, 50) : longer object length is not a multiple of shorter object length

Page 9: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• 곱셈도 덧셈과 비슷하다.

• %*% : 행렬의 곱셈

R의 데이터 구조 : Matrix의 계산

> a * 2 [,1] [,2] [,3]

[1,] 2 6 10 [2,] 4 8 12 > a * c(10,100)

[,1] [,2] [,3] [1,] 10 30 50 [2,] 200 400 600

> a %*% matrix(rep(0,6),nrow=3,ncol=2) [,1] [,2]

[1,] 0 0 [2,] 0 0

Page 10: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• diag() : diagonal components

• eigen() : eigen values, eigen vectors

R의 데이터 구조 : Matrix

> x = c(7,3,4,5,6,2,3,1,1) > a = matrix(x, ncol=3)

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

> diag(a) [1] 7 6 1

> eigen(a) $values [1] 11.6032778 3.0000000 -0.6032778

$vectors [,1] [,2] [,3]

[1,] -0.7823145 -0.5656854 -0.38748709 [2,] -0.4879469 0.7071068 0.03654409 [3,] -0.3871587 -0.4242641 0.92115052

Page 11: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• solve() : 역행렬

R의 데이터 구조 : Matrix의 계산

> solve(a) [,1] [,2] [,3]

[1,] -0.19047619 -0.04761905 0.6190476 [2,] -0.04761905 0.23809524 -0.0952381 [3,] 0.85714286 -0.28571429 -1.2857143

> solve(a) %*% a

Page 12: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• 행렬의 index : vector와 비슷하게 접근, [행,열]

R의 데이터 구조 : Matrix

> a[1,2] [1] 5

> a[1,4] Error in a[1, 4] : subscript out of bounds

> a[1,3] = 100 > a

[,1] [,2] [,3] [1,] 7 5 100[2,] 3 6 1 [3,] 4 2 1

> a[,3] = c(0,0,0) > a

[,1] [,2] [,3] [1,] 7 5 0[2,] 3 6 0[3,] 4 2 0

Page 13: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• 특징 : 여러 가지 속성의 자료를 묶을 수 있다.

• data.frame() : data frame을 생성

• str(), head(), tail() : data frame의 구조를 파악하는데 도움

R의 데이터 구조 : Data Frame

> age = c(13,14,5,3,40,50,55,32,27) > gender = factor( c("F","M","M","M","F","F","M","F","F") ) > mydata = data.frame(age, gender)

> class(mydata) [1] "data.frame"

> str(mydata) 'data.frame': 9 obs. of 2 variables: $ age : num 13 14 5 3 40 50 55 32 27 $ gender: Factor w/ 2 levels "F","M": 1 2 2 2 1 1 2 1 1

> head(mydata) > tail(mydata)

Page 14: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• 플레잉 카드덱(포커 카드) 만들기

• 구성 : Spade, Heart, Diamond, Club의 문양과 A, 2~10, J, Q, K

• 이 문제에서는 (A, 2~10, J, Q, K)를 (1~13)으로,

(Spade, Heart, Diamond, Club)를 (S, H, D, C)로 대체

• Hint : rep(), data.frame()

R에서의 데이터 정리 : 예제

S 1 S 2 S 3 S 4 S 5 S 6 S 7 S 8 ...

S 1 H 1 D 1 C 1 S 2 H 2 D 2 C 2...

Page 15: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• [,]으로 찾는 방법, vector 비슷하다.

• (data frame 이름)$(열이름) 으로 찾기

R의 데이터 구조 : Data Frame의 index

> mydata[1,] age gender

1 13 F

> mydata[,1] [1] 13 14 5 3 40 50 55 32 27

> head(mydata) > mydata$age [1] 13 14 5 3 40 50 55 32 27

Page 16: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• names() : data frame의 열 이름

* 대소문자 구분

R의 데이터 구조 : Data Frame의 name

> names(mydata) [1] "age" "gender" > names(mydata) = c("Age", "Gender")

> mydata$age NULL > mydata$Age [1] 13 14 5 3 40 50 55 32 27

Page 17: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• R의 기본 내장 데이터셋, cars 사용

• cars의 speed는 평균보다 크고, dist는 평균보다 작은 데이터셋은?

R의 데이터 구조 : Data Frame의 index 예제

> str(cars) 'data.frame': 50 obs. of 2 variables: $ speed: num 4 4 7 7 8 9 10 10 10 11 ... $ dist : num 2 10 4 22 16 10 18 26 34 17 ...

speed dist 27 16 32 28 16 40 29 17 32 30 17 40 32 18 42 36 19 36 39 20 32

Page 18: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• attach(), detach()

R의 데이터 구조 : Data Frame

> mydata = data.frame(age, gender) > rm("age","gender") > age Error: object 'age' not found

> attach(mydata) > search() [1] ".GlobalEnv" "mydata" "tools:rstudio“ ...

> age [1] 13 14 5 3 40 50 55 32 27

> detach(mydata) > search() [1] ".GlobalEnv" "tools:rstudio“ ...

> age Error: object 'age' not found

Page 19: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• list : 여러 가지 속성 데이터의 조합

• 결과 정리에 유용함. 다수의 속성을 가진 결과를 하나로 묶음

R의 데이터 구조 : List

> mylist = list(vec = 1:10, mat = a, df = mydata) > mylist $vec [1] 1 2 3 4 5 6 7 8 9 10

$mat [,1] [,2] [,3]

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

$df age gender

1 13 F 2 14 M ...

> class(mylist) [1] "list"

Page 20: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• 결측값(NA) 처리하기

• NA를 30으로 바꾸기, 단 인덱스 10을 이용하지 말 것

R에서의 데이터 정리 : 결측값 처리

> age = c(13,14,5,3,40,50,55,32,27, NA) > gender = factor( c("F","M","M","M","F","F","M","F","F","M")) > age [1] 13 14 5 3 40 50 55 32 27 NA > mydata = data.frame(age, gender)

> mydata$age[mydata$age == NA] = 30 > mydata$age [1] 13 14 5 3 40 50 55 32 27 NA

> mydata$age == NA [1] NA NA NA NA NA NA NA NA NA NA

> is.na(mydata$age) [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE

Page 21: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• TRUE와 FALSE

• 기본적으로 TRUE = 1, FALSE = 0이다.

• 추가로 0이 아닌 숫자는 TRUE로 인식한다

R에서의 데이터 정리 : 결측값 처리

> TRUE + TRUE [1] 2 > FALSE + FALSE + FALSE [1] 0

> as.logical( c(1,2,3,4,0,0,-1) ) [1] TRUE TRUE TRUE TRUE FALSE FALSE TRUE

Page 22: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• 한 행 혹은 여러 행을 기준으로 정렬

• 나이가 적은 순서대로 전체 데이터를 정렬

R에서의 데이터 정리 : 정렬하기

> mydata age gender

1 13 F 2 14 M 3 5 M

...

> # order() 사용

age gender 4 3 M 3 5 M 1 13 F

...

> sort(mydata) Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) : 정의하지 않은 열들이 선택되었습니다

Page 23: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• iris dataset : R에 기본으로 내장되어 있는 dataset

• 붓꽃의 종류에 대해서 꽃받침과 꽃잎의 너비와 길이로 이루어진 자료

1. Sepal.Length을 범주화 : sepal<6 경우 = 1, sepal>=6 경우 = 0

2. setosa종 중에서 sepal.width, petal.width가 작은 순서대로 나열

3. species의 범주를 변경 : setosa는 se, versicolor는 ve, virginica는 vi

4. 열이름(변수명)을 변경 : SL, SW, PL, PW, Species으로

R에서의 데이터 정리 : 예제

> str(iris) 'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 ... $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 ...

Page 24: Lec. 2 : Introduction to R Part 2statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University Lec. 2 : Introduction to R –Part 2 Big Data Analytics Short

Soongsil University

• airquality: R에 기본으로 내장되어 있는 dataset

• Daily air quality measurements in NY, May to September 1973.

1. Ozone의 결측값(NA) 갯수는?

2. 6개 변수들의 결측값이 없는 행의 갯수는?

3. Ozone의 NA를 999로 변경하기

R에서의 데이터 정리 : 예제

> str(airquality) 'data.frame': 153 obs. of 6 variables: $ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ... $ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ... $ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ... $ Temp : int 67 72 74 62 56 66 65 59 61 69 ... $ Month : int 5 5 5 5 5 5 5 5 5 5 ... $ Day : int 1 2 3 4 5 6 7 8 9 10 ...