Skip to contents

This function matches the elements of two numeric vectors as well as possible (that means with the smallest possible sum of absolute deviations).

Usage

match_numerics(x, y)

Arguments

x, y

[numeric()]
Two vectors of the same length without missing values.

Value

An integer vector of length length(x) with the positions of y in x, i.e., the \(i\)-th element is the position of the element of x that is matched with the \(i\)-th element of y (hence, x[match_numerics(x, y)] is aligned with y).

Examples

x <- c(-1, 0, 1)
y <- c(0.1, 1.5, -1.2)
match_numerics(x, y)
#> [1] 2 3 1
x[match_numerics(x, y)]
#> [1]  0  1 -1