Detect relations from Allen's Interval AlgebraSource:
- needles, haystack
Interval vectors used for relation matching.
Each element of
needlesrepresents the interval to search for.
haystackrepresents the intervals to search in.
Prior to comparison,
haystackare coerced to the same type.
These dots are for future extensions and must be empty.
The type of relationship to find. See the Allen's Interval Algebra section for a complete description of each type. One of:
[logical(1) / "equals" / "error"]
Handling of missing intervals in
"equals"considers missing intervals in
needlesas exactly equal to missing intervals in
haystackwhen determining if there is a matching relationship between them. Matched missing intervals in
needlesresult in a
TRUEvalue in the result, and unmatched missing intervals result in a
"error"throws an error if any intervals in
If a single logical value is provided, this represents the value returned in the result for intervals in
needlesthat are missing. You can force missing intervals to be unmatched by setting this to
FALSE, and you can force them to be propagated by setting this to
The interval algebra developed by James Allen serves as a basis and
iv_locate_follows(). The original algebra is composed of 13 relations
which have the following properties:
Distinct: No pair of intervals can be related by more than one
Exhaustive: All pairs of intervals are described by one of the
Qualitative: No numeric intervals are considered. The relationships are computed by purely qualitative means.
Take the notation that
y represent two intervals. Now assume that
x can be represented as
[x_s, x_e), where
x_s is the start of the
x_e is the end of it. Additionally, assume that
x_s < x_e.
With this notation, the 13 relations are as follows:
x_e < y_s
x_s > y_e
x_e == y_s
x_s == y_e
(x_s < y_s) & (x_e > y_s) & (x_e < y_e)
(x_e > y_e) & (x_s < y_e) & (x_s > y_s)
(x_s == y_s) & (x_e < y_e)
(x_s == y_s) & (x_e > y_e)
(x_s > y_s) & (x_e == y_e)
(x_s < y_s) & (x_e == y_e)
(x_s > y_s) & (x_e < y_e)
(x_s < y_s) & (x_e > y_e)
(x_s == y_s) & (x_e == y_e)
Note that when
missing = "equals", missing intervals will only match
type = "equals" relation. This ensures that the distinct property
of the algebra is maintained.
Note that some of the above relations are fairly restrictive. For example,
"overlaps" only detects cases where
y_s. It does not
consider the case where
y are equal to be an overlap (as this
"equals") nor does it consider when
y_e to be an
overlap (as this is
"overlapped-by"). This makes the relations extremely
useful from a theoretical perspective, because they can be combined without
fear of duplicating relations, but they don't match our typical expectations
for what an "overlap" is.
types that aren't distinct, but typically match your
expectations better. They can each be expressed in terms of Allen's
overlaps | overlapped-by | starts | started-by | finishes | finished-by | during | contains | equals
contains | started-by | finished-by | equals
during | starts | finishes | equals
starts | started-by | equals
finishes | finished-by | equals
precedes | meets
preceded-by | met-by
x <- iv(1, 3) y <- iv(3, 4) # `"precedes"` is strict, and doesn't let the endpoints match iv_relates(x, y, type = "precedes") #>  FALSE # Since that is what `"meets"` represents iv_relates(x, y, type = "meets") #>  TRUE # `"overlaps"` is a very specific type of overlap where an interval in # `needles` straddles the start of an interval in `haystack` x <- iv_pairs(c(1, 4), c(1, 3), c(0, 3), c(2, 5)) y <- iv(1, 4) # It doesn't match equality, or when the starts match, or when the end # of the interval in `haystack` is straddled instead iv_relates(x, y, type = "overlaps") #>  FALSE FALSE TRUE FALSE