Debian Codegraph @

man/which_nearest-methods.Rd

raw
1 \name{which_nearest}
2
3 \alias{which_nearest}
4 \alias{which_nearest,numeric,Intervals_virtual-method}
5 \alias{which_nearest,Intervals_virtual,Intervals_virtual-method}
6 \alias{which_nearest,Intervals_virtual,numeric-method}
7 \alias{which_nearest,numeric,numeric-method}
8
9 \title{Identify nearest member(s) in a set of intervals}
10
11 \description{
12 For each point or interval in the \code{from} argument,
13 identify the nearest member or members (in case of ties) of the
14 interval set in the \code{to} argument.
15 }
16
17 \usage{
18 \S4method{which_nearest}{numeric,Intervals_virtual}(from, to, check_valid = TRUE)
19
20 \S4method{which_nearest}{Intervals_virtual,numeric}(from, to, check_valid = TRUE)
21
22 \S4method{which_nearest}{Intervals_virtual,Intervals_virtual}(from, to, check_valid = TRUE)
23
24 \S4method{which_nearest}{numeric,numeric}(from, to, check_valid = TRUE)
25 }
26
27 \arguments{
28
29 \item{from}{An object of appropriate type.}
30
31 \item{to}{An object of appropriate type.}
32
33 \item{check_valid}{
34 Should \code{\link{validObject}} be called before passing to
35 compiled code? Also see \code{\link{interval_overlap}}.
36 }
37
38 }
39
40 \value{
41 A data frame with three columns: \code{distance_to_nearest},
42 \code{which_nearest}, and \code{which_overlap}. The last two are
43 actually lists, since there may be zero, one, or more
44 nearest/overlapping intervals in the \code{to} object for any given
45 interval in \code{from}.
46
47 Empty intervals in \code{to}, or intervals with \code{NA} endpoints,
48 produce a \code{NA} distance result, and no nearest or overlapping
49 hits.
50 }
51
52 \note{
53 (v. 0.11.0) The code used for the \code{distance_to_nearest} column
54 here is completely distinct from that used for the original
55 \code{\link{distance_to_nearest}} function. For the moment, they will
56 co-exist for testing purposes, but this function's code will
57 eventually replace the older code.
58
59 Note that a naive way of implementing \code{which_nearest} would be to
60 use the simpler, old implementation of \code{distance_to_nearest}, use
61 \code{expand} to grow all intervals by the correspnoding amount, and
62 then use \code{interval_overlap} to identify target. This approach,
63 however, will miss a small fraction of targets due to floating point
64 issues.
65 }
66
67 \examples{
68 # Point to interval. Empty rows, or those with NA endpoints, do not
69 # generate hits. Note that distance_to_nearest can be 0 but without
70 # overlap, depending on endpoint closure.
71
72 to <- Intervals_full( c(-1,0,NA,5,-1,3,10,Inf) )
73 closed(to)[1,] <- FALSE
74 closed(to)[2,2] <- FALSE
75 from <- c( NA, -3:5 )
76
77 to
78 cbind( from, which_nearest( from, to ) )
79
80 # Completely empty to object
81
82 which_nearest( from, to[1,] )
83
84 # Interval to interval
85
86 from <- Intervals( c(-Inf,-Inf,3.5,-1,1,4) )
87 from
88 which_nearest( from, to )
89
90 # Checking behavior with ties
91
92 from <- Intervals_full( c(2,2,4,4,3,3,5,5) )
93 closed( from )[2:3,] <- FALSE
94 to <- Intervals_full( c(0,0,6,6,1,1,7,8) )
95 closed( to )[2:3,] <- FALSE
96
97 from
98 to
99 which_nearest( from, to )
100
101 from <- Intervals_full( c(1,3,6,2,4,7) )
102 to <- Intervals_full( c(4,4,5,5) )
103 closed( to )[1,] <- FALSE
104
105 from
106 to
107 which_nearest( from, to )
108 }