我有一张这样的桌子:
而且我有一个像这样的图书馆:
我想使用表格匹配库以查找目标化合物,标准为RT偏差c(-0.5,0.5)和MZ偏差c(-5,5)。因此理想的结果将是:
或尝试在R控制台中输入:
df <- data.frame(peak = c(1:5), RT = c(3, 3.6, 4, 4.1, 5), MZ = c(100, 200, 900, 100, 700))
library <- data.frame(Compound = c("A","B","C","D","E","F","G","H"), RT = c(3.11, 3.2, 4, 4.1, 4.2, 4.4, 4.9, 5), MZ = c(101, 200, 500, 250, 300, 330, 701, 702))
另外,要向前推进,如果不用for循环就可以解决问题,因为我的实际列表很长...
如果要尝试使用这些数字,请参见xlsx文件https://drive.google.com/open?id=1W0bviCyIimox59Yeo8oIVNSgcIcrFVqJ
使用dplyr
的一种方法:
library(dplyr)
tidyr::crossing(library, setNames(df, c('peak', 'RT1', 'MZ1'))) %>%
filter(abs(RT - RT1) <= 0.5 & abs(MZ - MZ1) <= 5) %>%
group_by(peak) %>%
summarise(Compound = toString(Compound)) %>%
right_join(df, by = 'peak')