我需要帮助来匹配两列之间的两个文档,以便它选择文档 1 中的对,并循环遍历文档 2 查找匹配项并输出差异最小的匹配项(最佳可能匹配项)。例如,查看下面的数据,我希望输出位于文档 2 中,其中显示文档 2 中的匹配值
文件1
| A 栏 | B 栏 |
| -------- | -------- |
| 0.12 | 0.12 23.45 | 23.45
| 0.13 | 0.13 25.65 |
文件2 | A 栏 | B 栏 | | -------- | -------- | | 0.12 | 0.12 23.45 | 23.45 | 0.12 | 0.12 23.46 |
输出
A 栏 | B 栏 | 来自 doc2 的匹配 |
---|---|---|
细胞1 | 细胞2 | 0.12 |
细胞3 | 4 号电池 |
我开发了以下脚本来做到这一点。
r1
r2
matched_hold = list()
# Loop through rows in r1
for (row in r1$rowid) {
zval = r1$RTB[r1$rowid == row]
mval = r1$MZB[r1$rowid == row]
# Check if zval and mval are numeric
if (is.numeric(zval) && is.numeric(mval)) {
r2_copy = r2 %>%
filter((RTA < zval + 0.05 & RTA > zval - 0.05) &
(MZA < mval + 0.01 & MZA > mval - 0.01))
r2_copy$RTB = zval
r2_copy$MZB = mval
matched_hold[[row]] = r2_copy
}
}
# Combine the matched data frames
matched_df = do.call('rbind', matched_hold)
但是,该脚本是错误的,因为它没有带来最佳匹配,例如,如果有两个匹配,一个差异为 0.5,另一个差异为 0.1,则不会选择 0.1,而是会选择 0.5 匹配。您能否帮我修改它,以便结果返回符合所提供条件的最佳匹配基础。
嘿,我必须承认我对你举的例子感到困惑,让我重新表述一下,让事情变得更简单,这样我们就可以轻松地互相理解。我也会使用“循环”方法帮助您完成它。请随意更改它以满足您的特定需求
这里有我们要比较的两个数据框:
df1 = data.frame(RTA=c(1, 2.1, 0, NA), MZA=c(4, 5.1, 0, NA))
df2 = data.frame(RTB=c(1, 2, 3, NA), MZB=c(4, 5, 6, NA))
现在让我们执行您尝试过的循环方法,但做一些小改动
# You don't need to use an ID column for the loop, can be risky
matched_hold = list()
for (row_df1 in 1:nrow(df1)){
# Let's select the row values
rta_val = df1$RTA[row_df1]
mza_val = df1$MZA[row_df1]
# As you wanted, we only consider cases when both values are numeric (and not NA values!)
if (is.numeric(rta_val) & is.numeric(mza_val) & !is.na(rta_val+mza_val)){
# Instead of a nested loop we use a vectorial composition to get all differences values
diff_per_row = abs(df2$RTB - rta_val) + abs(df2$MZB - mza_val) # Differences should be absolute!
# Now, what is the cell most similar to this cell? You can find out
row_df2 = which.min(diff_per_row)
# Introduce that cell as the list value
matched_hold[[row_df1]] = row_df2
}
}