简体   繁体   English

比较r中的两个表

[英]Comparing two tables in r

I have a table with reference positions, like x is the start and y is the end. 我有一个参考位置的表,像x是开始,y是结束。

|---------------------|------------------|
|           x         |        y         |
|---------------------|------------------|
|          10         |         35       |
|---------------------|------------------|
|          58         |         89       |
|---------------------|------------------|

Then I have another table with single positions and my goal is to check if any of the positions in this second table are in the first table, considering that the positions in this second table can be in between the col1 and col2. 然后我有另一个单一位置的表,我的目标是检查第二个表中的任何位置是否在第一个表中,考虑到第二个表中的位置可以在col1和col2之间。

|---------------------|
|          12         |     
|---------------------|
|          27         |       
|---------------------|
|          65         |
|---------------------|

How can I check this, since I can't use any of the joins from dplyr , or even the unique . 我怎么能检查这个,因为我不能使用dplyr的任何连接 ,甚至是唯一的连接

We can use foverlaps from data.table 我们可以使用foverlapsdata.table

library(data.table)
df1 <- data.frame(x = c(10, 58), y = c(35, 89))
df2 <- data.frame(x= c(12, 27, 65), y = c(12, 27, 65))
setDT(df1, key = c('x', 'y'))
setDT(df2, key = c('x', 'y'))
foverlaps(df2, df1, type = "within", which = TRUE)$yid 
#[1] 1 1 2

Version 1.9.8 of data.table (on CRAN 25 Nov 2016) introduced non-equi joins which can be used instead of foverlaps() : 版本data.table (在CRAN 2016年11月25日)引入了非equi连接 ,可以用来代替foverlaps()

setDT(df1)[setDT(df2), on = .(x <= z, y >= z), which = TRUE]
 [1] 1 1 2 NA 

Note that the second table differs from OP's data as a fourth row has been added which doesn't match any of the intervals. 请注意,第二个表与OP的数据不同,因为添加了第四行,它与任何间隔都不匹配。

Data 数据

df1 <- data.frame(x = c(10, 58), y = c(35, 89))
df2 <- data.frame(z = c(12, 27, 65, 90))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM