繁体   English   中英

查找重叠范围和

[英]Finding overlapping ranges and

我有两个数据帧,其中包含两个在范围内报告的变量:mzmin,mzmed,mzmax,rtmin,rtmed和rtmax。 存在:

table1 <- read.csv("table1.csv")

name        mzmed       mzmin       mzmax       rtmed   rtmin   rtmax
M1          202.1110    202.110859  202.111285  50.35   49.62   51.13
M2          373.144219  373.143792  373.154876  50.38   49.62   51.86
M3          371.14497   371.144256  371.145224  80.34   79.62   81.41
M4          372.147279  372.146992  372.147583  100.35  99.62   101.41

table2 <- read.csv("table2.csv")

name        mzmed       mzmin       mzmax       rtmed   rtmin   rtmax
M1          558.109976  558.102886  558.111497  10.89   9.95    11.95
M2          371.144564  371.144000  371.144999  80.29   79.14   81.98
M3          498.091821  498.091632  498.092225  658.15  656.57  660.96
M4          284.098785  284.098429  284.099092  760.32  758.67  761.2

在这种情况下,我想要将表1的M3和表2的M2写入新表,因为mz范围重叠

如果M2和M3的rt范围小于100,则也仅将它们写入新表将是有益的。 我认为IRanges将以某种方式得到最佳使用,但我并不乐观。

任何帮助或建议,将不胜感激。

正如Uwe Block所评论的那样,foverlaps起作用了。

table1 <- data.table(read.table(header = T, 
                   text = "name        mzmed       mzmin       mzmax       rtmed   rtmin   rtmax
M1          202.1110    202.110859  202.111285  50.35   49.62   51.13
                   M2          373.144219  373.143792  373.154876  50.38   49.62   51.86
                   M3          371.14497   371.144256  371.145224  80.34   79.62   81.41
                   M4          372.147279  372.146992  372.147583  100.35  99.62   101.41
"))

table2 <- data.table(read.table(header = T, 
                     text = "name        mzmed       mzmin       mzmax       rtmed   rtmin   rtmax
M1          558.109976  558.102886  558.111497  10.89   9.95    11.95
M2          371.144564  371.144000  371.144999  80.29   79.14   81.98
M3          498.091821  498.091632  498.092225  658.15  656.57  660.96
M4          284.098785  284.098429  284.099092  760.32  758.67  761.2
"))

setkey(table2, mzmin, mzmax)
out <- foverlaps(table1, table2, type="any",nomatch=0L)

> out
   name    mzmed   mzmin   mzmax rtmed rtmin rtmax i.name i.mzmed  i.mzmin  i.mzmax i.rtmed i.rtmin i.rtmax
1:   M2 371.1446 371.144 371.145 80.29 79.14 81.98     M3 371.145 371.1443 371.1452   80.34   79.62   81.41

如果您希望mz的范围在rt的范围内100,则可以使用以下代码:

out[abs(mzmin-rtmax)<100 | abs(rtmin-mzmax)<100,]
Empty data.table (0 rows) of 14 cols: name,mzmed,mzmin,mzmax,rtmed,rtmin...

暂无
暂无

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

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