Skip to contents

Which values are nearest to any given quantiles

Usage

near_quantile(x, probs, tol = 0.01)

Arguments

x

vector

probs

quantiles to calculate

tol

tolerance in terms of x that you will accept near to the quantile. Default is 0.01.

Value

logical vector of TRUE/FALSE if number is close to a quantile

Examples

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

library(dplyr)
heights %>% 
  features(height_cm, list(min = min)) %>% 
  mutate(min_near_median = near_quantile(min, 0.5, 0.01)) %>%
  filter(min_near_median)
#> # A tibble: 0 × 3
#> # … with 3 variables: country <chr>, min <dbl>, min_near_median <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