[英]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.