psolymos / intrval

Relational Operators for Intervals
41 stars 4 forks source link

How to match elements of a vector to a data frame of intervals. #14

Closed benmarwick closed 7 years ago

benmarwick commented 7 years ago

This looks like a really useful set of functions. I often need to match elements of a vector to a data frame of intervals. Currently I use the IRanges package, but I wonder if your package might be simpler to use. Can you tell me if there is a more efficient method with your package? This is what I've got so far:

# Find which interval that each element of the vector belongs in

library(tidyverse)
# here are my elements
elements <- c(0.1, 0.2, 0.5, 0.9, 1.1, 1.9, 2.1)

# here are my intervals
intervals <- 
  frame_data(  ~phase, ~start, ~end,
               "a",     0,      0.5,
               "b",     1,      1.9,
               "c",     2,      2.5
  )

# For each element, I want to know what interval does it belong in
library(intrval)
map(elements, ~.x %[]% data.frame(intervals[, c('start', 'end')])) %>% 
  map(., ~unlist(intervals[.x, 'phase'])) 

The output is like this, which I'm happy with:

[[1]]
phase 
  "a" 

[[2]]
phase 
  "a" 

[[3]]
phase 
  "a" 

[[4]]
character(0)

[[5]]
phase 
  "b" 

[[6]]
phase 
  "b" 

[[7]]
phase 
  "c" 

But I'm wondering if there's a simpler way to use your functions so I don't need the two map functions.

Thanks!

psolymos commented 7 years ago

@benmarwick I cannot see a much simpler way unless you have [a,b) type intervals so that you can use cut to bin the values. But that might also be problematic if the intervals overlap.