Skip to contents

Returns TRUE if x is nearest to y. There are two implementations. nearest_lgl() returns a logical vector when an element of the first argument is nearest to an element of the second argument. nearest_qt_lgl() is similar to nearest_lgl(), but instead determines if an element of the first argument is nearest to some value of the given quantile probabilities. See example for more detail.

Usage

nearest_lgl(x, y)

nearest_qt_lgl(y, ...)

Arguments

x

a numeric vector

y

a numeric vector

...

(if used) arguments to pass to quantile().

Value

logical vector of length(y)

Examples


x <- 1:10
y <- 5:14
z <- 16:25
a <- -1:-5
b <- -1

nearest_lgl(x, y)
#>  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
nearest_lgl(y, x)
#>  [1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

nearest_lgl(x, z)
#>  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
nearest_lgl(z, x)
#>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

nearest_lgl(x, a)
#> [1]  TRUE FALSE FALSE FALSE FALSE
nearest_lgl(a, x)
#>  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

nearest_lgl(x, b)
#> [1] TRUE
nearest_lgl(b, x)
#>  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

library(dplyr)
heights_near_min <- heights %>%
  filter(nearest_lgl(min(height_cm), height_cm))
  
heights_near_fivenum <- heights %>%
  filter(nearest_lgl(fivenum(height_cm), height_cm))
  
heights_near_qt_1 <- heights %>%
  filter(nearest_qt_lgl(height_cm, c(0.5)))
  
heights_near_qt_3 <- heights %>%
  filter(nearest_qt_lgl(height_cm, c(0.1, 0.5, 0.9)))