범위 제약 조건

R로 데이터 정리하기

Maggie Matsui

Content Developer @ DataCamp

범위 밖 값이란?

  • SAT 점수: 400–1600
  • 포장 무게: 최소 0 lb/kg
  • 성인 심박수: 분당 60–100회
R로 데이터 정리하기

범위를 벗어난 값 찾기

movies
  title                 avg_rating
  <chr>                      <dbl>
1 A Beautiful Mind             4.1
2 La Vita e Bella              4.3
3 Amelie                       4.2
4 Meet the Parents             3.5
5 Unbreakable                  5.8
6 Gone in Sixty Seconds        3.3
...
R로 데이터 정리하기

범위를 벗어난 값 찾기

breaks <- c(min(movies$avg_rating), 0, 5, max(movies$avg_rating))

ggplot(movies, aes(avg_rating)) + geom_histogram(breaks = breaks)

코드로 생성된 3개 구간 히스토그램: 너무 낮은 구간 1개, 범위 내 6개, 너무 높은 값 2개.

R로 데이터 정리하기

범위를 벗어난 값 찾기

library(assertive)
assert_all_are_in_closed_range(movies$avg_rating, lower = 0, upper = 5)
Error: is_in_closed_range : movies$avg_rating are not all in the range [0,5].
There were 3 failures:
  Position Value    Cause
1        5   5.8 too high
2        8   6.2 too high
3        9  -4.4  too low
R로 데이터 정리하기

범위를 벗어난 값 처리

  • 행 제거
  • 결측값(NA)으로 처리
  • 범위 한계값으로 대체
  • 도메인/데이터셋 지식으로 다른 값으로 대체
R로 데이터 정리하기

행 제거하기

movies %>%
  filter(avg_rating >= 0, avg_rating <= 5) %>%


ggplot(aes(avg_rating)) + geom_histogram(breaks = c(min(movies$avg_rating), 0, 5, max(movies$avg_rating)))

코드로 생성된 히스토그램: 범위 내 값 6개, 범위 아래/위 값 0개.

R로 데이터 정리하기

결측으로 처리하기

movies
  title                 avg_rating
  <chr>                      <dbl>
1 A Beautiful Mind             4.1
2 La Vita e Bella              4.3
3 Amelie                       4.2
4 Meet the Parents             3.5
5 Unbreakable                  5.8
6 Gone in Sixty Seconds        3.3
...

replace(col, condition, replacement)

movies %>%
  mutate(rating_miss = 
    replace(avg_rating, avg_rating > 5, NA))
  title                rating_miss
  <chr>                      <dbl>
1 A Beautiful Mind             4.1
2 La Vita e Bella              4.3
3 Amelie                       4.2
4 Meet the Parents             3.5
5 Unbreakable                   NA
6 Gone in Sixty Seconds        3.3
...
R로 데이터 정리하기

범위 밖 값을 대체하기

movies %>%
  mutate(rating_const = 
           replace(avg_rating, avg_rating > 5, 5))
  title               rating_const
  <chr>                      <dbl>
1 A Beautiful Mind             4.1
2 La Vita e Bella              4.3
3 Amelie                       4.2
4 Meet the Parents             3.5
5 Unbreakable                  5.0
6 Gone in Sixty Seconds        3.3
...
R로 데이터 정리하기

날짜 범위 제약 조건

assert_all_are_in_past(movies$date_recorded)
Error: is_in_past : movies$date_recorded are not all in the past.
There was 1 failure:
  Position               Value     Cause
1        3 2064-09-22 20:00:00 in future
library(lubridate)
movies %>%
  filter(date_recorded > today())
    title  avg_rating  date_recorded
1  Amelie         4.2  2064-09-23
R로 데이터 정리하기

범위를 벗어난 날짜 제거

library(lubridate)
movies <- movies %>%
  filter(date_recorded <= today())
library(assertive)
assert_all_are_in_past(movies$date_recorded)


기억하세요, 출력 없음 = 통과!

R로 데이터 정리하기

Lass uns üben!

R로 데이터 정리하기

Preparing Video For Download...