Ràng buộc phạm vi

Làm sạch dữ liệu trong R

Maggie Matsui

Content Developer @ DataCamp

Giá trị ngoài phạm vi là gì?

  • Điểm SAT: 400–1600
  • Cân nặng gói hàng: ít nhất 0 lb/kg
  • Nhịp tim người lớn: 60–100 nhịp/phút
Làm sạch dữ liệu trong R

Tìm giá trị ngoài phạm vi

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
...
Làm sạch dữ liệu trong R

Tìm giá trị ngoài phạm vi

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

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

Biểu đồ tần suất ba cột do mã tạo, với một giá trị quá thấp, sáu giá trị trong phạm vi, và hai giá trị quá cao.

Làm sạch dữ liệu trong R

Tìm giá trị ngoài phạm vi

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
Làm sạch dữ liệu trong R

Xử lý giá trị ngoài phạm vi

  • Xóa hàng
  • Xem như thiếu (NA)
  • Thay bằng giới hạn phạm vi
  • Thay bằng giá trị khác theo hiểu biết miền và/hoặc bộ dữ liệu
Làm sạch dữ liệu trong R

Xóa hàng

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)))

Biểu đồ tần suất do mã tạo với sáu giá trị trong phạm vi, và 0 giá trị dưới hoặc trên giới hạn.

Làm sạch dữ liệu trong R

Xem như thiếu

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
...
Làm sạch dữ liệu trong R

Thay thế giá trị ngoài phạm vi

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
...
Làm sạch dữ liệu trong R

Ràng buộc phạm vi ngày

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
Làm sạch dữ liệu trong R

Xóa ngày ngoài phạm vi

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


Nhớ: không có output = đã qua!

Làm sạch dữ liệu trong R

Vamos praticar!

Làm sạch dữ liệu trong R

Preparing Video For Download...