Closest: Find the Closest Value

Description Usage Arguments Value Author(s) See Also Examples

View source: R/DescTools.r

Description

Find the closest value(s) of a number in a vector x. Multiple values will be reported, if the differences are the same.

Usage

1
Closest(x, a, which = FALSE, na.rm = FALSE)

Arguments

x

the vector to be searched in

a

the reference value

which

a logical value defining if the index position or the value should be returned. By default will the value be returned.

na.rm

a logical value indicating whether NA values should be stripped before the computation proceeds.

Value

the value or index in x which is closest to a

Author(s)

Andri Signorell <andri@signorell.net>

See Also

which

Examples

1
2
3
4
5
6
7
8
set.seed(8)
x <- sample(10, size=10, replace=TRUE)

Closest(x, 6)
Closest(x, 6, which=TRUE)


Closest(c(2, 3, 4, 5), 3.5)

AndriSignorell/DescTools documentation built on April 8, 2021, 5:51 a.m.