Return x percent to y percent of values

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] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE #> [13] FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
library(dplyr) wages %>% features(ln_wages, list(min = min)) %>% filter(near_between(min, 0.1, 0.9))
#> # A tibble: 710 x 2 #> id min #> <int> <dbl> #> 1 31 1.43 #> 2 36 1.80 #> 3 53 1.54 #> 4 145 1.48 #> 5 155 1.54 #> 6 173 1.56 #> 7 207 1.58 #> 8 222 1.31 #> 9 223 1.51 #> 10 234 1.54 #> # … with 700 more rows
near_quantile(x = x, probs = 0.5, tol = 0.01)
#> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
near_quantile(x, c(0.25, 0.5, 0.75), 0.05)
#> [1] TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE #> [13] TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
wages %>% features(ln_wages, l_five_num) %>% mutate_at(vars(min:max), .funs = near_quantile, 0.5, 0.01) %>% filter(min)
#> # A tibble: 30 x 6 #> id min q_25 med q_75 max #> <int> <lgl> <lgl> <lgl> <lgl> <lgl> #> 1 145 TRUE FALSE FALSE FALSE FALSE #> 2 1417 TRUE FALSE TRUE TRUE FALSE #> 3 1515 TRUE FALSE FALSE FALSE FALSE #> 4 1535 TRUE FALSE FALSE FALSE FALSE #> 5 1731 TRUE FALSE FALSE FALSE FALSE #> 6 1772 TRUE FALSE FALSE FALSE FALSE #> 7 2213 TRUE FALSE FALSE FALSE FALSE #> 8 3272 TRUE FALSE FALSE FALSE FALSE #> 9 6860 TRUE FALSE FALSE FALSE FALSE #> 10 6918 TRUE FALSE FALSE FALSE FALSE #> # … with 20 more rows
wages %>% features(ln_wages, 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: 66 x 3 #> id min min_near_q3 #> <int> <dbl> <lgl> #> 1 145 1.48 TRUE #> 2 1417 1.48 TRUE #> 3 1515 1.48 TRUE #> 4 1535 1.48 TRUE #> 5 1580 1.64 TRUE #> 6 1595 1.63 TRUE #> 7 1672 1.63 TRUE #> 8 1731 1.48 TRUE #> 9 1772 1.48 TRUE #> 10 1811 1.64 TRUE #> # … with 56 more rows
wages %>% features(ln_wages, list(min = min)) %>% filter(near_between(min, 0.1, 0.9))
#> # A tibble: 710 x 2 #> id min #> <int> <dbl> #> 1 31 1.43 #> 2 36 1.80 #> 3 53 1.54 #> 4 145 1.48 #> 5 155 1.54 #> 6 173 1.56 #> 7 207 1.58 #> 8 222 1.31 #> 9 223 1.51 #> 10 234 1.54 #> # … with 700 more rows
wages %>% features(ln_wages, list(min = min)) %>% filter(near_middle(min, 0.5, 0.1))
#> # A tibble: 88 x 2 #> id min #> <int> <dbl> #> 1 145 1.48 #> 2 316 1.46 #> 3 324 1.49 #> 4 356 1.49 #> 5 517 1.49 #> 6 735 1.46 #> 7 1300 1.49 #> 8 1417 1.48 #> 9 1515 1.48 #> 10 1535 1.48 #> # … with 78 more rows