Description Usage Arguments Details Value Note Examples
This function uses tools in the intervals package to quickly identify clusters – contiguous collections of positions or intervals which are separated by no more than a given distance from their neighbors to either side.
1 2 3 4 5 |
x |
An appropriate object. |
w |
Maximum permitted distance between a cluster member and its neighbors to either side. |
which |
Should indices into the |
check_valid |
Should |
A cluster is defined to be a maximal collection, with at least two
members, of components of x
which are separated by no more than
w
. Note that when x
represents intervals, an interval
must actually contain a point at distance w
or less from
a neighboring interval to be assigned to the same cluster. If the ends
of both intervals in question are open and exactly at distance
w
, they will not be deemed to be cluster co-members. See the
example below.
A list whose components are the clusters. Each component is thus a
subset of x
, or, if which == TRUE
, a vector of
indices into the x
object. (The indices correspond to row
numbers when x
is of class "Intervals_virtual"
.)
Implementation is by a call to reduce
followed by a call
to interval_overlap
. The clusters
methods are
included to illustrate the utility of the core functions in the
intervals package, although they are also useful in their own
right.
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 31 32 33 34 35 36 37 38 | # Numeric method
w <- 20
x <- sample( 1000, 100 )
c1 <- clusters( x, w )
# Check results
sapply( c1, function( x ) all( diff(x) <= w ) )
d1 <- diff( sort(x) )
all.equal(
as.numeric( d1[ d1 <= w ] ),
unlist( sapply( c1, diff ) )
)
# Intervals method, starting with a reduced object so we know that all
# intervals are disjoint and sorted.
B <- 100
left <- runif( B, 0, 1e4 )
right <- left + rexp( B, rate = 1/10 )
y <- reduce( Intervals( cbind( left, right ) ) )
gaps <- function(x) x[-1,1] - x[-nrow(x),2]
hist( gaps(y), breaks = 30 )
w <- 200
c2 <- clusters( y, w )
head( c2 )
sapply( c2, function(x) all( gaps(x) <= w ) )
# Clusters and open end points. See "Details".
z <- Intervals(
matrix( 1:4, 2, 2, byrow = TRUE ),
closed = c( TRUE, FALSE )
)
z
clusters( z, 1 )
closed(z)[1] <- FALSE
z
clusters( z, 1 )
|
[1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE
[1] "Mean relative difference: 4.630363"
[[1]]
Object of class Intervals
2 intervals over R:
[49.5773483999074, 52.0277032510596]
[75.9917474351823, 87.541734566492]
[[2]]
Object of class Intervals
9 intervals over R:
[457.756505347788, 459.774326104247]
[496.461454313248, 507.684265760595]
[518.49014358595, 527.160131986764]
[570.407593622804, 574.225898971781]
[658.580029848963, 661.543098930269]
[715.704413596541, 722.870855637028]
[792.076119687408, 828.710038284718]
[891.967662610114, 904.920610161078]
[965.555591974407, 996.292903874379]
[[3]]
Object of class Intervals
9 intervals over R:
[1216.5997363627, 1222.63050168969]
[1233.33005234599, 1237.70793007584]
[1414.39638566226, 1429.82473878062]
[1477.64207795262, 1479.66833456419]
[1506.46146619692, 1517.90378661784]
[1537.42291498929, 1566.34565669351]
[1584.32842930779, 1603.46492461776]
[1670.82790052518, 1693.25489986562]
[1729.52014487237, 1733.46721378255]
[[4]]
Object of class Intervals
7 intervals over R:
[1947.96900497749, 1951.96615788405]
[2112.37646872178, 2121.34804146364]
[2136.61299319938, 2140.84633076098]
[2197.92055897415, 2199.52057803515]
[2262.00601784512, 2269.51407848999]
[2273.59689073637, 2276.31266938057]
[2356.81728459895, 2360.01373758371]
[[5]]
Object of class Intervals
9 intervals over R:
[2881.42230827361, 2881.91030388341]
[2913.45037752762, 2923.94389179665]
[2978.92853152007, 2990.81385229672]
[3015.57872444391, 3016.06194117572]
[3149.48669169098, 3149.93769725614]
[3157.20543032512, 3166.26946381243]
[3182.62605229393, 3184.35991792008]
[3344.33614742011, 3350.0189188635]
[3475.43268697336, 3491.86482991939]
[[6]]
Object of class Intervals
5 intervals over R:
[3740.27110869065, 3753.75273380387]
[3868.6072290875, 3884.60329933759]
[3965.84260743111, 3966.56889855166]
[4083.35220767185, 4087.91779448278]
[4246.11666705459, 4249.06228741631]
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Object of class Intervals
2 intervals over R:
[1, 2)
[3, 4)
[[1]]
Object of class Intervals
2 intervals over R:
[1, 2)
[3, 4)
Object of class Intervals
2 intervals over R:
(1, 2)
(3, 4)
list()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.