# [R] partitioning vectors of intervals

jim holtman jholtman at gmail.com
Sun Sep 28 04:32:53 CEST 2008

```Will this do it for you:

> set.seed(1)
> coarse <- runif(5,0,100)
> coarse <- cbind(coarse, coarse + runif(5,1,10))
> coarse <- round(coarse,3)
> fine <- runif(100,0,100)
> fine <- cbind(fine, fine + runif(100,0,1))
> result <- lapply(seq(nrow(coarse)), function(.row){
+     list(coarse[.row,], which(fine[,2] > coarse[.row,1] & fine[,1] <
coarse[.row, 2]))
+ })
> lapply(result, function(x){
+     paste(paste(x[[1]], collapse=','), '[', paste(x[[2]], collapse=','), ']')
+ })
[[1]]
[1] "26.551,35.636 [ 15,20,47,52,54,61,63,64,74,92,93 ]"

[[2]]
[1] "37.212,46.714 [ 4,9,16,18,30,43,50,53,68,71,73,87,88 ]"

[[3]]
[1] "57.285,64.233 [ 22,83,90,95,100 ]"

[[4]]
[1] "90.821,97.483 [ 11,51,70,99 ]"

[[5]]
[1] "20.168,21.724 [ 1,12,76,96 ]"

You can change the comparison around; this is for ones that overlap,
not wholey contained.

On Sat, Sep 27, 2008 at 8:10 PM, Alexy Khrabrov <deliverable at gmail.com> wrote:
> I have two pairs of time intervals: coarse- and fine-grained.  They're
> components of their respective dataframes, looking like,
>
> coarse:    endtime            starttime
> 1                t1_end             t1_start
>  2               t2_end             t2_start
> ...
>
> fine: is the same, except that its intervals presumably fall into the
> coarse's enclosing ones.
>
> The problem is to partition the fine intervals into the coarse ones, adding
> a list to each row showing which fine intervals fall in it, like
>
> coarse: ... fine
> 1                [1,2]
> 2                [3,4,5]
>
> Is there a functional way to do this?
> Cheers,
> Alexy
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

```