32
Ch. 3,4 Matrix / Factor 유덕 R Moon.D

R 2주차 matrix : factor

Embed Size (px)

Citation preview

Page 1: R 2주차 matrix : factor

Ch. 3,4 Matrix / Factor유덕

R Moon.D

Page 2: R 2주차 matrix : factor

Moon.D

질문&토론과 함께~

R

Page 3: R 2주차 matrix : factor

Today

Ch. 3 행렬(Matrix)Ch. 4 요소(Factor)

Moon.DR

Page 4: R 2주차 matrix : factor

Matrix

Moon.DR

Page 5: R 2주차 matrix : factor

Matrix = 행렬

Moon.DR

Page 6: R 2주차 matrix : factor

Moon.DR

Matrix

ㄱ나니..?

Page 7: R 2주차 matrix : factor

Moon.DR

Matrix

row

column

Page 8: R 2주차 matrix : factor

matrix()

?matrix

Moon.DR

• matrix creates a matrix from the given set of values

Page 9: R 2주차 matrix : factor

matrix()Moon.DR

• matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)

nrow = row 갯수 / ncos = column 갯수

byrow = row 기준?

dimnames = 행, 열의 명칭들

str(matrix)

Page 10: R 2주차 matrix : factor

matrix 만들기

matrix(1:24, nrow=3, ncol=8, byrow=TRUE)

Moon.DR

Q. 1부터 24까지 3행 8열로 만들어보기 / 행 기준 정렬

Q. 1부터 24까지 3행 8열로 만들어보기 / 열 기준 정렬

matrix(1:24, nrow=3, ncol=8, byrow=FALSE)

Page 11: R 2주차 matrix : factor

Moon.DR

문디 실습.R

Page 12: R 2주차 matrix : factor

> star1

US non-USA New Hope 461.0 314.4The Empire Strikes Back 290.5 247.9Return of the Jedi 309.3 165.8

Moon.DR

Page 13: R 2주차 matrix : factor

rowSums()Moon.DR

US non-USA New Hope 461.0 314.4The Empire Strikes Back 290.5 247.9Return of the Jedi 309.3 165.8

World <- rowSums(star1)

A New Hope The Empire Strikes Back Return of the Jedi 775.4 538.4 475.1

Page 14: R 2주차 matrix : factor

cbind(_,_)Moon.DR

column을 기준으로 묶어준다.

Q. star1, World를 cbind()으로 묶어보자

Page 15: R 2주차 matrix : factor

rbind(_,_)Moon.DR

row를 기준으로 묶어준다.

Q. star1, star2를 rbind()으로 묶어보자

Q. star2를 출력해보자

Page 16: R 2주차 matrix : factor

colSums()

Moon.DR

column의 총합

rowSums()row의 총합

Page 17: R 2주차 matrix : factor

Moon.DR

Q. star1에서 US, non-US 총합 각각?

Q. star1에서 영화 1,2,3편 수익 총합 각각?

Page 18: R 2주차 matrix : factor

mean()Moon.DR

평균

Q. star1에서 US 평균?

Q. star1에서 Return of the Jedi(3편)의 평균?

Page 19: R 2주차 matrix : factor

matrix 연산Moon.DR

+ - x /

Q. star1에서 모든 영화 표값이 10. 관람객 수?

Page 20: R 2주차 matrix : factor

Factor

Moon.DR

Page 21: R 2주차 matrix : factor

Factor = 요소?

Moon.DR

Page 22: R 2주차 matrix : factor

Factor in R= categorical variable

Moon.DR

Page 23: R 2주차 matrix : factor

데이터 유형Moon.DR

Data

Quantitative(numerical)

Qualitative(categorical)

Discrete(count) Continuous OrdinalNominal

Binary

한국 방문자 수 몸무게, 키 머리 색 상/ 중/ 하

남성 / 여성

Page 24: R 2주차 matrix : factor

factor()

?factor

Moon.DR

• factor() is used to encode a vector as a factor

Page 25: R 2주차 matrix : factor

binary factor()Moon.DR

> factor(gender_vector)

gender_vector <- c("Male", "Female", "Female", "Male", "Male")

Page 26: R 2주차 matrix : factor

nominal factor()Moon.DR

> factor(animals_vector)

animals_vector <- c("Elephant", "Giraffe", "Donkey", "Horse")

Page 27: R 2주차 matrix : factor

ordianl factor()Moon.DR

> factor(temperature_vector, order=TRUE, levels = c("Low", "Medium", "High"))

temperature_vector <- c("High", "Low", "High","Low", "Medium")

Page 28: R 2주차 matrix : factor

factor level 이름 변경

levels(factor_vector) <- c(“name1”, “name2”, …)

Moon.DR

1. gen 출력해보기2. factor(gen) 만들기3. gen의 levels “Female”, “Male”로 바꿔보기

Page 29: R 2주차 matrix : factor

summary()Moon.DR

• produce result summaries of the results of various model

?summary

Page 30: R 2주차 matrix : factor

summary(x)Moon.DR

c(1, 4, 6, 9, 10 ,14, 17, 19, 20, 23, 26, 30, 43)

x <- c(1, 4, 6, 9, 10 ,14, 17, 19, 20, 23, 26, 30, 43)

Median MaxMin 1st Qu. 3st Qu.

Page 31: R 2주차 matrix : factor

Moon.DR

Q. sang “하” < “중” < “상” ordinal factor 만들기

Q. “상” > “하” 맞는지 테스트하기

Quizsang <- c("중", "상", "하")

((미리 정의 돼있음))

Page 32: R 2주차 matrix : factor

Moon.D

수고하셨습니다

R