Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... ·...

21
Soongsil University Lec. 3 : 유용한 기능 및 함수 Big Data Analytics Short Course 17. 07. 05

Transcript of Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... ·...

Page 1: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

Lec. 3 : 유용한 기능 및 함수

Big Data Analytics Short Course

17. 07. 05

Page 2: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• ; : 한 문장의 마침 표시

R의 유용한 기능 및 함수

> a = 1 > b = 10 > a+b [1] 11 > a = 1; b = 10; a+b [1] 11

> a = 1; b = 10; a+b [1] 11

> a = 1:10; b=2:11; data.frame(a,b) a b 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 ...

Page 3: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• () : 한 문장의 결과를 실행과 동시에 보여준다.

• # : 주석, 명령어를 설명하는 개념, 실행은 되지 않는다.

R의 유용한 기능 및 함수

> a = 10; a [1] 10 > (a = 10) [1] 10

> e = exp(c(1,2,3,4,5)); e [1] 2.718282 7.389056 20.085537 54.598150 148.413159 > ( e = exp(c(1,2,3,4,5)) ) [1] 2.718282 7.389056 20.085537 54.598150 148.413159

> 5+3 [1] 8 > 5+3 # 덧셈하는 명령어 [1] 8

Page 4: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• table() : 빈도를 계산하는 함수

• 위의 결과를 정렬하기

• data frame의 table

R의 유용한 기능 및 함수 : table()

> table(c("A","A","A","B","C","C","A","B","C","C","C")) A B C 4 2 5

> freq = table(c("A","A","A","B","C","C","A","B","C","C","C")) > sort(freq) B A C 2 4 5

> a = c("A","A","A","B","C","C","A","B","C","C","C") > b = c(1,1,2,3,1,4,5,2,3,3,1) > freq = data.frame(a,b) > table(freq) b a 1 2 3 4 5 A 2 1 0 0 1 B 0 1 1 0 0 C 2 0 2 1 0

Page 5: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• sample() : 벡터의 원소를 sampling하는 명령어

• 복원추출을 하고 싶다면 replace=TRUE

• 확률을 다르게 부여하고 싶다면 prob= 이용하기

• 확인해보기

R의 유용한 기능 및 함수 : sample()

> sample(1:100, 6, replace=FALSE) [1] 18 95 84 79 56 20

> sample(c("H","T"), 10, replace=TRUE) [1] "T" "T" "H" "H" "T" "H" "T" "T" "H" "T"

> sample(c("H","T"), 10, replace=TRUE, prob=c(9,1)) [1] "H" "H" "T" "H" "H" "T" "H" "H" "H" "H"

> test1 = sample(c("H","T"), 10000, replace=TRUE) > test2 = sample(c("H","T"), 10000, replace=TRUE, prob=c(9,1)) > table(test1) > table(test2)

Page 6: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• %in% : 벡터의 해당 값이 존재하는지 확인

• paste() : character를 붙여주는 함수

R의 유용한 기능 및 함수

> ( x = sample(1:100, 6) ) [1] 15 20 43 53 39 93 > x %in% 1 [1] FALSE FALSE FALSE FALSE FALSE FALSE > x %in% 15 [1] TRUE FALSE FALSE FALSE FALSE FALSE > x %in% c(1,15,93) [1] TRUE FALSE FALSE FALSE FALSE TRUE

> chr = c("b","a","n","a","n","a") > paste(chr, collapse="") [1] "banana" > paste(chr, collapse="/") [1] "b/a/n/a/n/a"

Page 7: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• rbind(), cbind()

• 여러 벡터를 하나로 묶어주는 함수, matrix

• 물론 길이가 다르면 문제가 생긴다

R의 유용한 기능 및 함수

> rbind(1:10, 2:11) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3 4 5 6 7 8 9 10 [2,] 2 3 4 5 6 7 8 9 10 11 > cbind(1:10, 2:11) [,1] [,2] [1,] 1 2 [2,] 2 3 ...

> rbind(1:10, 2:4) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3 4 5 6 7 8 9 10 [2,] 2 3 4 2 3 4 2 3 4 2 Warning message: In rbind(1:10, 2:4) : number of columns of result is not a multiple of vector length (arg 2)

Page 8: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• apply()

• 참고) iris[,-5] 의 의미는?

• apply(x, margin, function) 형태에서,

행 별로 function을 적용시키고 싶으면 margin=1로 지정,

열 별로 function을 적용시키고 싶으면 margin=2로 지정

• 그 외에도 없는 함수를 만들어서 사용할 수도 있다.

R의 유용한 기능 및 함수

> apply(iris[,-5], 2, mean) Sepal.Length Sepal.Width Petal.Length Petal.Width 5.843333 3.057333 3.758000 1.199333 > apply(iris[,-5], 2, min) Sepal.Length Sepal.Width Petal.Length Petal.Width 4.3 2.0 1.0 0.1

> apply(iris[,-5], 1, min) > apply(iris[,-5], 2, function(x) x*0 + 1)

Page 9: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• apply()

• margin=1

• margin=2

R의 유용한 기능 및 함수

func func func func func

func

func

func

func

func

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

> apply(x,1,sum) > apply(x,1,exp)

Page 10: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• install.packages(), library()

• 패키지를 설치하고, 불러들이는 함수

• 외부 데이터 불러오기, 내보내기

• read.table(), write.table() / read.csv(), write.csv()

R의 유용한 기능 및 함수

> write.csv(iris, "C:/Users/HSS/Desktop/a.csv") > ( dat = read.csv("C:/Users/HSS/Desktop/a.csv") )

> install.packages(“UsingR”) > search() [1] “.GlobalEnv” “tools:rstudio” ... > library(UsingR) > search() [1] “.GlobalEnv” “package:UsingR" "tools:rstudio“...

Page 11: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• d, p, q, r functions

• R에는 분포와 관련하여 4가지 유형의 함수가 존재한다.

• d() : pdf (probability distribution function)

• p() : cdf (cumulative distribution function)

• q() : quantile function

• r() : random generation function

R의 분포함수

Page 12: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• Normal distribution

• X ~ N(0,1) 일때,

• dnorm()

R의 분포함수

> dnorm(1) [1] 0.2419707

Page 13: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• pnorm()

R의 분포함수

> pnorm(1) [1] 0.8413447

Page 14: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• qnorm()

R의 분포함수

> pnorm(1.64) [1] 0.9494974 > qnorm(0.95) [1] 1.644854

Page 15: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• rnorm()

• N(0,1)이 아닌 다른 parameter를 사용하고 싶은 경우

• norm(x, mean=num1, sd=num2)과 같은 형식을 사용

R의 분포함수

> rnorm(10) [1] -0.04941364 -0.10192955 1.54519454 -0.75504973 -0.36816778 -0.66037823 0.64565658 0.02935836 -1.03044536 -2.25882315

> rnorm(10,5,10) [1] 5.260847 -6.002726 -2.968946 2.612414 17.413257 -3.674142 8.362099 7.190095 19.691288 7.250460

Page 16: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• R에 있는 여러 가지 분포 함수

R의 분포함수

Distribution Name Parameters

binomial binom size, prob

Poisson pois lambda

geometric geom prob

hypergeometric hyper m, n, k

negative binomial nbinom size, prob

uniform unif min, max

gamma gamma shape, rate

beta beta shape1, shape2

exponential exp rate

chi-squared chisq df

normal norm mean, sd

log-normal lnorm meanlog,sdlog

logistic logis location, scale

T t df

F f df1, df2

Page 17: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• 주머니에 공이 10개 있다. Black 4개, Red 3개, White 1개, Green 2개.

이 때 복원추출로 공을 3개 꺼내는 코드는?

• N(0,1)에서 난수 1000개를 생성하고 이 중 1보다 작은 값의 비율은?

* hint : 비율 = 해당하는 값의 갯수/전체 갯수

• 알파벳 26개 중 랜덤으로 6개를 선택해서 단어를 만들어보기

• 주사위를 1000번 던지고, 그 중 6이 몇 개인지 확인하기

R의 유용한 기능 및 함수 : 예제

Page 18: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• 조건문 if

> if(condition) expr1 else expr2 , else 이하는 생략가능

* condition이 TRUE이면 expr1 실행, FALSE이면 expr2 실행

R의 유용한 기능 및 함수 : 조건문

> x = -3 > if(x < 0){ + x = -x + } > x [1] 3

> x = -3 > if(x < 0){ + print("x는 0보다 작습니다.") + }else{ + print("x는 0보다 크거나 같습니다.") + } [1] "x는 0보다 작습니다."

Page 19: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• 반복문 for

> for( variable in vector ){ expr }

R의 유용한 기능 및 함수 : 반복문

> x = -(1:10) > for( i in c(2,4,6,8) ){ + print(x[i]) + } [1] -2 [1] -4 [1] -6 [1] -8

> for( a in c("H","T")){ + print(a) + } [1] "H" [1] "T"

Page 20: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• 반복문 while

> while( condition ){ expr }

• 반복문 repeat

> repeat{ expr; if() break }

R의 유용한 기능 및 함수 : 반복문

> a = 0 > while(a<10){ a = a+1; print(a) } [1] 1 [1] 2 ...

> a = 1 > repeat{ print(a); a = a+1; if(a>10) break }

Page 21: Lec. 3 : 유용한 기능 및 함수statistics.ssu.ac.kr/~CC/SIT/courses/2017summer... · 2017-07-04 · Soongsil University • sample() : 벡터의 원소를 sampling하는 명령어

Soongsil University

• 알파벳 26개 중 랜덤으로 3개를 선택해서 단어를 만들 때, 그 단어가

“YES”가 되는 시행횟수를 구하기

* hint : LETTERS dataset, repeat, paste

• 카이제곱분포(자유도 4)에서 1000개의 난수를 만들고, 그 난수의 평균

을 계산. 이 과정을 10000번 반복하여 나온 결과로 히스토그램 그리기

* 히스토그램 그리는 함수 : hist()

R의 유용한 기능 및 함수 : 예제