Description Usage Arguments Value Examples
For each element of from
, find the the closest element of within
.
1 2 3 |
from |
a numeric vector (or coercible as such); the values to be matched. |
within |
a vector of the same nature as |
outside |
logicial. When |
A vector of integers of the same length as from
: for closest()
, the value of the closest elements of within
; for which.closest()
the indexes of those values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | set.seed(1)
to_match <- round(runif(5, min=0, max=100), 1)
reference <- round(runif(10, min=20, max=80), 1)
# compare the vectors
to_match
reference
# get closest mactches, or their index
closest(to_match, within=reference)
which.closest(to_match, within=reference)
# verify graphically
plot(
x=c(reference, to_match),
y=c(rep(2, 10), rep(1, 5)), ylim=c(0, 3), yaxt="n",
xlab="", ylab=""
)
axis(2, at=1:2, labels=c("to match", "reference"))
# join closest points
segments(
x0=to_match, y0=1,
x1=closest(from=to_match, within=reference), y1=2
)
# join closest points inside "within"
abline(v=range(reference), lty="22")
segments(
x0=to_match, y0=1,
x1=closest(from=to_match, within=reference, outside=FALSE), y1=2,
col="red")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.