Skip to contents

Return x percent to y percent of values

Usage

near_between(x, from, to)

Arguments

x

numeric vector

from

the lower bound of percentage

to

the upper bound of percentage

Value

logical vector

Examples

x <- runif(20)

near_middle(x = x,
            middle = 0.5,
            within = 0.2)
#>  [1]  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [13] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE

library(dplyr)
heights %>% features(height_cm, list(min = min)) %>%
  filter(near_between(min, 0.1, 0.9))
#> # A tibble: 114 × 2
#>    country       min
#>    <chr>       <dbl>
#>  1 Afghanistan  161.
#>  2 Albania      168.
#>  3 Algeria      166.
#>  4 Argentina    167.
#>  5 Armenia      164.
#>  6 Austria      162.
#>  7 Bahrain      161.
#>  8 Bangladesh   160.
#>  9 Belarus      164.
#> 10 Belgium      163.
#> # ℹ 104 more rows

near_quantile(x = x,
              probs = 0.5, 
              tol = 0.01)
#>  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [13] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE

near_quantile(x, c(0.25, 0.5, 0.75), 0.05)
#>  [1]  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE
#> [13] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE

heights %>%
  features(height_cm, l_five_num) %>%
  mutate_at(vars(min:max),
            .funs = near_quantile,
            0.5, 
            0.01) %>%
  filter(min)
#> # A tibble: 0 × 6
#> # ℹ 6 variables: country <chr>, min <lgl>, q_25 <lgl>, med <lgl>, q_75 <lgl>,
#> #   max <lgl>

heights %>%
  features(height_cm, list(min = min)) %>%
  mutate(min_near_q3 = near_quantile(min, c(0.25, 0.5, 0.75), 0.01)) %>%
  filter(min_near_q3)
#> # A tibble: 2 × 3
#>   country      min min_near_q3
#>   <chr>      <dbl> <lgl>      
#> 1 Ethiopia    161. TRUE       
#> 2 Madagascar  161. TRUE       

heights %>%
  features(height_cm, list(min = min)) %>%
  filter(near_between(min, 0.1, 0.9))
#> # A tibble: 114 × 2
#>    country       min
#>    <chr>       <dbl>
#>  1 Afghanistan  161.
#>  2 Albania      168.
#>  3 Algeria      166.
#>  4 Argentina    167.
#>  5 Armenia      164.
#>  6 Austria      162.
#>  7 Bahrain      161.
#>  8 Bangladesh   160.
#>  9 Belarus      164.
#> 10 Belgium      163.
#> # ℹ 104 more rows

heights %>%
  features(height_cm, list(min = min)) %>%
  filter(near_middle(min, 0.5, 0.1))
#> # A tibble: 14 × 2
#>    country       min
#>    <chr>       <dbl>
#>  1 Brazil       164.
#>  2 Cameroon     164.
#>  3 Estonia      165.
#>  4 Gabon        164.
#>  5 Ghana        164.
#>  6 Guinea       164.
#>  7 Kenya        165.
#>  8 Kyrgyzstan   164.
#>  9 Latvia       165.
#> 10 Lithuania    165.
#> 11 Netherlands  164.
#> 12 Switzerland  165.
#> 13 Tajikistan   165.
#> 14 Uganda       165.