2017-05-21 188 views
0

我有兩個數據框,其中包含以範圍報告的兩個變量: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 

在這種情況下,Table 1和Table的M2 M3我想是因爲MZ範圍重疊被寫入到一個新表。

如果M2和M3的rt範圍小於100,它們也只能寫入新表中,這將是有益的。我假設IRanges不知何故會被最好地使用,但我並不積極。

任何幫助或建議,將不勝感激。

+3

也許,'foverlaps()'從'data.table '包可能適合你。 – Uwe

回答

1

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的範圍是內室溫範圍內的100,那麼你可以使用下面的代碼:

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