रेंज constraints

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)

कोड से बने तीन बिन वाला histogram: एक मान बहुत कम, छह मान रेंज में, और दो मान बहुत ज़्यादा।

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 में डेटा क्लीनिंग

रेंज से बाहर मानों को संभालना

  • पंक्तियाँ हटाएँ
  • missing मान (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)))

कोड से बना histogram: छह मान रेंज में, और रेंज से नीचे/ऊपर 0 मान।

R में डेटा क्लीनिंग

missing मान मानें

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 में डेटा क्लीनिंग

तिथि रेंज constraints

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 में डेटा क्लीनिंग

अभ्यास करते हैं!

R में डेटा क्लीनिंग

Preparing Video For Download...