closest_to_target: Get index of element in vector with value closest to a target

View source: R/misc.R

closest_to_targetR Documentation

Get index of element in vector with value closest to a target

Description

Get index of element in vector with value closest to a target

Usage

closest_to_target(vector, target)

Arguments

vector

Identify element in this numeric vector

target

numeric target

Value

an integer indexing vector

Examples

closest_to_target(c(0.1, 0.2, 0.3), 0.05)  # 1
closest_to_target(c(0.1, 0.2, 0.3), 0.22)  # 2
closest_to_target(c(0.1, 0.2, 0.3), -0.05) # 1
closest_to_target(c(0.1, 0.2, 0.3), 8) # 3

trialr documentation built on April 1, 2023, 12:03 a.m.