closest_to_target {trialr}R 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

[Package trialr version 0.1.6 Index]