使用R按因子级别查找重叠范围

使用R按因子级别查找重叠范围,r,R,我需要在单个数据集中找到重叠的范围,但需要为每个ID或因子级别找到它们。任何帮助都将不胜感激 library(dplyr) df_foo = read.table( textConnection("Class Min Max A 500 630 A 100 200 B 100 200 A 210 310 A 200 210 B 210 310 A 510 530 B 200 210 A 705 800

我需要在单个数据集中找到重叠的范围,但需要为每个ID或因子级别找到它们。任何帮助都将不胜感激

    library(dplyr)

df_foo = read.table(
  textConnection("Class    Min  Max
A    500  630
A    100  200
B    100  200
A    210  310
A    200  210
B    210  310
A    510  530
B    200  210
A    705  800
B    500  630
B    510  530
B    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 
  )
但我想找出A和B的每个级别之间的重叠,如下所示:

   Class Min Max Overlap
1      A 500 630    TRUE
2      A 100 200    FALSE
3      B 100 200    FALSE
4      A 210 310    FALSE
5      A 200 210    FALSE
6      B 210 310    FALSE
7      A 510 530    TRUE
8      B 200 210    FALSE
9      A 705 800    FALSE
10     B 500 630    TRUE
11     B 510 530    TRUE
12     B 705 800    FALSE

使用
dplyr

df=df_foo%>%group_by(Class)%>%
  mutate(Overlap=if_else(Min<lag(Max,order_by=Class),TRUE,FALSE))
df$Overlap[which(df$Overlap==TRUE)-1]=TRUE
df$Overlap[which(is.na(df$Overlap))]=FALSE

> df
# A tibble: 12 x 4
# Groups:   Class [2]
   Class   Min   Max Overlap
   <fct> <dbl> <dbl> <lgl>  
 1 A       100   200 FALSE  
 2 A       200   210 FALSE  
 3 A       210   310 FALSE  
 4 A       500   630 TRUE   
 5 A       510   530 TRUE   
 6 A       705   800 FALSE  
 7 B       100   200 FALSE  
 8 B       200   210 FALSE  
 9 B       210   310 FALSE  
10 B       500   630 TRUE   
11 B       510   530 TRUE   
12 B       705   800 FALSE 
df=df\u foo%%>%group\u by(Class)%%>%
变异(重叠=if_else(最小df
#一个tibble:12x4
#组别:班级[2]
类最小最大重叠
1 A 100 200错误
2 A 200 210错误
3 A 210 310错误
4 A 500 630正确
5 A 510 530正确
6 A 705 800假
7 B 100 200错误
8B200210错误
9 B 210 310假
10 B 500 630正确
11 B 510 530正确
12 B 705 800错误
此代码假定您的值按升序排列,因为它只检查前一行

编辑
不是最漂亮的,而是有效的

df_foo$Class=as.character.factor(df_foo$Class)
df_foo=as.data.frame(df_foo)
df_foo$Overlap=rep("FALSE",nrow(df_foo))
for (i in 1:nrow(df_foo)){
  aux=FALSE
  class=df_foo$Class[i]
  df=df_foo[-i,]%>%filter(.,Class==class)
  for (j in 1:nrow(df)){
    if (df_foo[i,"Min"]<df[j,"Max"] & df_foo[i,"Max"] > df[j,"Min"]){
      aux=TRUE
    }
  }
  df_foo[i,"Overlap"]=aux
}


> df_foo
   Class Min Max Overlap
1      A 500 630    TRUE
2      A 100 200   FALSE
3      B 100 200   FALSE
4      A 210 310   FALSE
5      A 200 210   FALSE
6      B 210 310   FALSE
7      A 510 530    TRUE
8      B 200 210   FALSE
9      A 705 800   FALSE
10     B 500 630    TRUE
11     B 510 530    TRUE
12     B 705 800   FALSE
df_foo$Class=as.character.factor(df_foo$Class)
df_foo=as.data.frame(df_foo)
df_foo$Overlap=rep(“假”,nrow(df_foo))
对于(i in 1:nrow(df_foo)){
aux=错误
class=df_foo$class[i]
df=df_foo[-i,]%>%filter(,Class==Class)
对于(1中的j:nrow(df)){
if(df_foo[i,“Min”]df[j,“Min”]){
aux=真
}
}
df_foo[i,“重叠”]=aux
}
>德福
类最小最大重叠
1 A 500 630正确
2 A 100 200错误
3B100 200错误
4 A 210 310错误
5 A 200 210错误
6B210310错误
7 A 510 530正确
8B200210错误
9 A 705 800假
10 B 500 630正确
11 B 510 530正确
12 B 705 800错误

必须有一种方法可以使用
dplyr
来实现,但我无法找到它。所发生的事情是,它循环通过
df_foo
的每一行;它生成一个
dataframe
与同一组的所有其他行,并比较是否有重叠(
min我在
数据中得到了答案。表
,翻译成
dplyr
应该是straigtworfard。其想法是为每个类别创建一个先前累积最大值的向量:

df_foo <- setDT(df_foo)
df_foo[, shiftedmaxmax := c(NA,cummax(Max)[1:(.N-1)]),by = Class  ]


    Class Min Max shiftedmaxmax
 1:     A 100 200            NA
 2:     A 200 210           200
 3:     A 210 310           210
 4:     A 500 630           310
 5:     A 510 530           630
 6:     A 705 800           630
 7:     B 100 200            NA
 8:     B 200 210           200
 9:     B 210 310           210
10:     B 500 630           310
11:     B 510 530           630
12:     B 705 800           630

另一种
数据表
方法。
在这个答案中,样本数据/范围的顺序是不相关的…
foverlaps()
为您做了所有艰苦的工作

样本数据

library( data.table )
dt <- as.data.table( df_foo )
库(data.table)

dt谢谢,我本应该更清楚,但在我的实际数据集中,所有值都是混合的,不遵循升序或降序。是否有多个重叠?如果没有,只需按
按升序重新排列,代码就可以了。如果没有,应该清楚地更改。可能有多个重叠p、 我编辑了该示例以显示数据是如何混淆的。感谢您的帮助!谢谢,不幸的是,范围不是按升序或降序排列的。它们都混淆了。我编辑了数据集以反映这一点。
df_foo[,superposed := Min < shiftedmaxmax]

    Class Min Max shiftedmaxmax superposed
 1:     A 100 200            NA         NA
 2:     A 200 210           200      FALSE
 3:     A 210 310           210      FALSE
 4:     A 500 630           310      FALSE
 5:     A 510 530           630       TRUE
 6:     A 705 800           630      FALSE
 7:     B 100 200            NA         NA
 8:     B 200 210           200      FALSE
 9:     B 210 310           210      FALSE
10:     B 500 630           310      FALSE
11:     B 510 530           630       TRUE
12:     B 705 800           630      FALSE
df_foo[,superposedsource :=  Max %in% shiftedmaxmax[superposed],by = Class]
df_foo[,superposedtot := ifelse((superposed | superposedsource) &,T,F)]

    Class Min Max shiftedmaxmax superposed superposedsource superposedtot
 1:     A 100 200            NA         NA            FALSE            NA
 2:     A 200 210           200      FALSE            FALSE         FALSE
 3:     A 210 310           210      FALSE            FALSE         FALSE
 4:     A 500 630           310      FALSE             TRUE          TRUE
 5:     A 510 530           630       TRUE            FALSE          TRUE
 6:     A 705 800           630      FALSE            FALSE         FALSE
 7:     B 100 200            NA         NA            FALSE            NA
 8:     B 200 210           200      FALSE            FALSE         FALSE
 9:     B 210 310           210      FALSE            FALSE         FALSE
10:     B 500 630           310      FALSE             TRUE          TRUE
11:     B 510 530           630       TRUE            FALSE          TRUE
12:     B 705 800           630      FALSE            FALSE         FALSE
library( data.table )
dt <- as.data.table( df_foo )
#set key for the data.table
setkey(dt, Min, Max)
#perform overlap join, keep only joined ranges where the class is the same, and Min and Max are not the same.
result <- foverlaps( dt, dt )[ Class == i.Class & !(Min == i.Min | Max == i.Max | Min == i.Max | Max == i.Min), ]
#create a logical vector (i.e. Overlap) by checking if the (pasted) combination of
#Class, Min and Max exists in both 'dt' and 'result'
dt[ , Overlap := paste0( Class, Min, Max ) %in% paste0( result$Class, result$Min, result$Max) ][]

#     Class Min Max Overlap
#  1:     A 100 200   FALSE
#  2:     B 100 200   FALSE
#  3:     A 200 210   FALSE
#  4:     B 200 210   FALSE
#  5:     A 210 310   FALSE
#  6:     B 210 310   FALSE
#  7:     A 500 630    TRUE
#  8:     B 500 630    TRUE
#  9:     A 510 530    TRUE
# 10:     B 510 530    TRUE
# 11:     A 705 800   FALSE
# 12:     B 705 800   FALSE