View source: R/which_closest.R
which_closest | R Documentation |
Function to find closest index in a vector.
which_closest(vector, value, na.rm = FALSE)
vector |
Vector to find closest value within. |
value |
Value to find closest indices for. Must be a vector with length of 1. |
na.rm |
Should |
Integer vector.
which
, which.min
NA
Stuart K. Grange
# A vector with no 17
x <- c(
15, 15.5, 16, 16, 16, 18, 18, 18, 18, 18, 18.5, 19, 19.5, 20, 20, 20, 20.5,
21, 21, 21, 21, 21
)
# Using base which returns an empty integer
which(x == 17)
# All values which are closest
which_closest(x, 17)
# Just a single value which is closest
which_closest(x, 17)[1]
# Get element, use as a filter
x[which_closest(x, 17)[1]]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.