Learn R Programming

intervals (version 0.15.5)

distance_to_nearest: Compute distance to nearest position in a set of intervals

Description

For each point or interval in the from argument, compute the distance to the nearest position in the to argument.

Usage

# S4 method for Intervals_virtual_or_numeric,Intervals_virtual_or_numeric
distance_to_nearest(from, to, check_valid = TRUE)

Value

A vector of distances, with one entry per point or interval in

from. Any intervals in from which are either empty (see

empty) or have NA endpoints produce a NA

result.

Arguments

from

An object of appropriate type.

to

An object of appropriate type.

check_valid

Should validObject be called before passing to compiled code? Also see interval_overlap.

See Also

See which_nearest, which also returns indices for the interval or intervals (in case of ties) at the distance reported.

Examples

Run this code
# Point to interval

to <- Intervals( c(0,5,3,Inf) )
from <- -5:10
plot( from, distance_to_nearest( from, to ), type = "l" ) 
segments( to[,1], 1, pmin(to[,2], par("usr")[2]), 1, col = "red" )

# Interval to interval

from <- Intervals( c(-Inf,-Inf,3.5,-1,1,4) )
distance_to_nearest( from, to )

Run the code above in your browser using DataLab