데이터 타입 제약

R로 데이터 정리하기

Maggie Matsui

Content Developer @ DataCamp

강의 개요

돋보기와 "더러운 데이터 진단"이 있는 서버

R로 데이터 정리하기

강의 개요

"더러운 데이터의 부작용" 버그 그림이 있는 서버

R로 데이터 정리하기

강의 개요

빗자루와 반짝이는 깨끗한 원형 데이터베이스

R로 데이터 정리하기

강의 개요

더러운 데이터 진단, 부작용, 정제

1장 - 흔한 데이터 문제

R로 데이터 정리하기

왜 깨끗한 데이터가 필요할까요?

 

데이터 과학 워크플로: 데이터 접근, 탐색/처리, 인사이트 도출, 보고

R로 데이터 정리하기

왜 깨끗한 데이터가 필요할까요?

 

인적/기술적 오류

R로 데이터 정리하기

왜 깨끗한 데이터가 필요할까요?

 

워크플로 전반으로 전파되는 오류

R로 데이터 정리하기

데이터 타입 제약

데이터 타입 예시
텍스트 이름, 성, 주소, ...
정수 구독자 수, 판매 제품 수, ...
소수 온도, 환율, ...
이진 기혼 여부, 신규 고객, 예/아니오, ...
범주 혼인 상태, 색상, ...
날짜 주문일, 생년월일, ...
R 데이터 타입
character
integer
numeric
logical
factor
Date
R로 데이터 정리하기

데이터 타입 훑어보기

sales <- read.csv("sales.csv")
head(sales)
  order_id revenue quantity
1     7432   5,454      494
2     7808   5,668      334
3     4893   4,062      259
4     6107   3,936       15
5     7661   1,067      307
6     5908   6,635      235
library(dplyr)
glimpse(sales)
Observations: 100
Variables: 3
$ order_id <dbl> 7432, 7808, ...
$ revenue  <chr> "$5454", "$5668", ...
$ quantity <dbl> 494, 334, ...
R로 데이터 정리하기

데이터 타입 점검

is.numeric(sales$revenue)
FALSE
library(assertive)
assert_is_numeric(sales$revenue)
Error: is_numeric : sales$revenue is not of class 'numeric'; it has class 'character'.
assert_is_numeric(sales$quantity)


R로 데이터 정리하기

데이터 타입 점검

논리 점검 - TRUE/FALSE 반환

  • is.character()
  • is.numeric()
  • is.logical()
  • is.factor()
  • is.Date()
  • ...

assertive 점검 - FALSE이면 에러

  • assert_is_character()
  • assert_is_numeric()
  • assert_is_logical()
  • assert_is_factor()
  • assert_is_date()
  • ...
R로 데이터 정리하기

데이터 타입이 중요한 이유

class(sales$revenue)
"character"
mean(sales$revenue)
NA
Warning message:
In mean.default(sales$revenue) :
  argument is not numeric or logical: returning NA
R로 데이터 정리하기

쉼표 문제

sales$revenue
"5,454" "5,668" "4,062" "3,936" "1,067" ...

R로 데이터 정리하기

문자열을 숫자로 변환

library(stringr)
revenue_trimmed = str_remove(sales$revenue, ",")

revenue_trimmed
"5454" "5668" "4062" "3936" "1067" ...
as.numeric(revenue_trimmed)
5454 5668 4062 3936 1067 ...
R로 데이터 정리하기

종합 적용

sales %>%
  mutate(revenue_usd = as.numeric(str_remove(revenue, ",")))
# A tibble: 100 x 4
   order_id revenue quantity revenue_usd
      <dbl> <chr>      <dbl>       <dbl>
 1     7432 5,454        494        5454
 2     7808 5,668        334        5668
 3     4893 4,062        259        4062
 4     6107 3,936         15        3936
 5     7661 1,067        307        1067
# ... with 95 more rows
R로 데이터 정리하기

같은 함수, 다른 결과

mean(sales$revenue)
NA
Warning message:
In mean.default(sales$revenue) :
  argument is not numeric or logical: returning NA
mean(sales$revenue_usd)
5361.4
R로 데이터 정리하기

데이터 타입 변환

  • as.character()
  • as.numeric()
  • as.logical()
  • as.factor()
  • as.Date()
  • ...
R로 데이터 정리하기

주의: factor → numeric

product_type
1000 1000 3000 2000 3000
Levels: 1000 2000 3000
class(product_type)
"factor"
as.numeric(product_type)
1 1 3 2 3
as.numeric(as.character(product_type))
1000 1000 3000 2000 3000
R로 데이터 정리하기

Ayo berlatih!

R로 데이터 정리하기

Preparing Video For Download...