Learn R Programming

trialr (version 0.1.6)

closest_to_target: 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)

Value

an integer indexing vector

Arguments

vector

Identify element in this numeric vector

target

numeric target

Examples

Run this code
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

Run the code above in your browser using DataLab