Overlap ranges in a single data frame

I am looking for a flag in my df that has overlapping ranges (to create an overlap column) based on a series of numeric variables (Min, Max) that I could convert to an integer if necessary:

Class Min Max A 100 200 A 120 205 A 210 310 A 500 630 A 510 530 A 705 800 

Convert to:

 Class Min Max Overlap A 100 200 1 A 120 205 1 A 210 310 0 A 500 630 1 A 510 530 1 A 705 800 0 

I tried IRanges without much success - any ideas?

+5
source share
3 answers

outer is my select function for quick pairwise comparisons. You can create a pairwise comparison of the endpoints of an interval using outer , and then combine the comparisons in any way. In this case, I check whether both the rules necessary for overlapping are fulfilled simultaneously.

 library(dplyr) df_foo = read.table( textConnection("Class Min Max A 100 200 A 120 205 A 210 310 A 500 630 A 510 530 A 705 800"), header = TRUE ) c = outer(df_foo$Max, df_foo$Min, ">") d = outer(df_foo$Min, df_foo$Max, "<") df_foo %>% mutate(Overlap = apply(c & d, 1, sum) > 1 ) 
+2
source

I find data.table very effective for overlays using foverlaps

  library(data.table) 

Recreating data:

 dt <- data.table(Class = c("A", "A", "A", "A", "A", "A"), Min = c(100, 120, 210, 500, 510, 705), Max = c(200, 205, 310, 630, 530, 800)) 

The data.table key is required for the function:

 setkey(dt, Min, Max) 

here we do foverlaps against ourselves, then filter by deleting those lines that overlap with foverlaps . The number of rows is then considered grouped by Min and Max .

 dt_overlaps <- foverlaps(dt, dt, type = "any")[Min != i.Min & Max != i.Max, .(Class, Overlap = .N), by = c("Min", "Max")] 

Thanks David Arenburg

 dt[dt_overlaps, Overlap := 1] 

Results:

 > dt Class Min Max Overlap 1 A 100 200 1 2 A 120 205 1 3 A 210 310 NA 4 A 500 630 1 5 A 510 530 1 6 A 705 800 NA 

There is probably a tidier data.table code for this, but I'm learning too.

+3
source
 library(dplyr) df_foo%>%mutate(flag=coalesce(ifelse(Max>lead(Min),1,NA),ifelse(lag(Max)>Min,1,NA))) Class Min Max flag 1 A 100 200 1 2 A 120 205 1 3 A 210 310 NA 4 A 500 630 1 5 A 510 530 1 6 A 705 800 NA 
0
source

Source: https://habr.com/ru/post/1258464/


All Articles