using int_overlaps() to determine whether any of a set of time intervals overlap

Skip to first unread message


Jul 13, 2014, 5:46:02 PM7/13/14
Hi Lubridate folks.

I am using lubridate to work with a vector of intervals like this miniature example:

int1 <- new_interval(ymd_hms("2014-07-12 05:48:02"), ymd_hms("2014-07-13 05:48:02"))
int2 <- new_interval(ymd_hms("2014-07-12 10:48:02"), ymd_hms("2014-07-13 07:48:02"))
int3 <- new_interval(ymd_hms("2014-07-11 10:48:02"), ymd_hms("2014-07-12 01:48:02"))
ints <- c(int1, int2, int3)

I would like to determine whether any combination of two intervals in the vector overlap with each other.  The int_overlaps() function does not seem to have a way to check all combinations.  Is there a way to do this that I missed?

int_overlaps(int1, int2)
int_overlaps(int1, ints)
int_overlaps(ints, ints)

Or, in the absence of a way to do it within the int_overlaps() function, is there a way to vectorize this operation, so as to avoid the use of a for-loop?  Ideally, I would like the results to be in the form of a matrix of TRUE or FALSE for each combination of intervals in the vector.



Garrett Grolemund

Jul 16, 2014, 8:56:01 AM7/16/14

I do not think there is a simple way to do what you want to do. int_overlaps can only compare two intervals at a time. It *is* vectorized (so all of your examples should work), but it is not designed to do what you describe.

It sounds like you would like a function that says whether any two intervals in a group overlap, and also tells you which intervals overlap. I do not know of such a function, but you could use int_overlaps as a building block to build it.


Bonnie Dixon

Jul 16, 2014, 12:51:26 PM7/16/14
Thanks for the reply, Garret.  I created a function that uses int_overlaps within a for-loop to do this, like so:

check_overlaps <- function(intervals) {
  results <- 
    matrix(rep(NA, length(intervals)*length(na.omit(intervals))), 
  for (i in 1:length(intervals)) {
    results[i, ] <- int_overlaps(intervals[i], na.omit(intervals))

      [,1]  [,2]  [,3]

This function is not fast, but it does the job.  (I omitted NA's from one side of the comparison because trying to compare two NA intervals to each other caused an error for some reason.  But keeping the NA's on one side of the comparison means that the resulting matrix retains the same index numbers for the observations as the original vector, so the overlapping intervals identified can be easily located.) 

Perhaps this functionality of int_overlaps should be considered for future lubridate development, since I would expect that the need for this operation would come up from time to time.


You received this message because you are subscribed to a topic in the Google Groups "lubridate" group.
To unsubscribe from this topic, visit
To unsubscribe from this group and all its topics, send an email to
For more options, visit

Reply all
Reply to author
0 new messages