概率链接 R 后去重
Deduplicate after probabilistic linkage R
我刚刚对两个数据集进行了概率关联。名为 "data" 的输出数据集包含来自两个原始数据集 ID_A 和另一个 ID_B 的标识号,以及链接分数 "match_score"。
ID_A<-c('123','124','125','125','126','127','127','128','129')
ID_B<-c('777','778','787','799','762','762','777','999','781')
Match_score<-c(28.1,15.6,19.7,18.9,36.1,55.1,28.7,19.5,18.2)
data<-data.frame(ID_A,ID_B,Match_score)
ID_A 和 ID_B 的组合有很多种。我只想 select 配对顶部链接,然后将它们从 selection 进程中删除以进行进一步链接。理想的输出是...
ID_A ID_B Match_score
127 762 55.1
123 777 28.1
125 787 19.7
128 999 19.5
129 781 18.2
124 778 15.6
ID_A:126 不匹配,因为 ID_B (762),match_score 对于另一个 ID_A (127) 更高。
ID_B:799 不匹配,因为 ID_A(125) 有一个更大的 match_score 和 (787)
如有任何帮助,我们将不胜感激!
我在 SAS 中找到了问题的解决方案,但是我在转换为 R 时遇到困难。
proc sort data=one;
by descending match_score ID_A ID_B;
run;
data want;
if _n_=1 then do;
dcl hash ha();
ha.definekey('ID_A');
ha.definedone();
dcl hash hb();
hb.definekey('ID_B');
hb.definedone();
end;
set one;
if ha.check()*hb.check() then do;
output;
ha.add();
hb.add();
end;
run;
我试图遵循您的逻辑。即使下面的代码看起来有点乱,我认为这是使用 base R
.
的一种解决方案
map_A <- data[duplicated(data$ID_A),]$ID_A
for(i in map_A) {
temp <- data[data$ID_A== i,]
index <- row.names(temp[which.min(temp$Match_score),])
data <- data[row.names(data)!= index,]
}
map_B <-data[duplicated(data$ID_B),]$ID_B
for(i in map_B) {
temp <- data[data$ID_B== i,]
index <- row.names(temp[which.min(temp$Match_score),])
data <- data[row.names(data)!= index,]
}
data[order(-data$Match_score),]
给予,
ID_A ID_B Match_score
127 762 55.1
123 777 28.1
125 787 19.7
128 999 19.5
129 781 18.2
124 778 15.6
我刚刚对两个数据集进行了概率关联。名为 "data" 的输出数据集包含来自两个原始数据集 ID_A 和另一个 ID_B 的标识号,以及链接分数 "match_score"。
ID_A<-c('123','124','125','125','126','127','127','128','129')
ID_B<-c('777','778','787','799','762','762','777','999','781')
Match_score<-c(28.1,15.6,19.7,18.9,36.1,55.1,28.7,19.5,18.2)
data<-data.frame(ID_A,ID_B,Match_score)
ID_A 和 ID_B 的组合有很多种。我只想 select 配对顶部链接,然后将它们从 selection 进程中删除以进行进一步链接。理想的输出是...
ID_A ID_B Match_score
127 762 55.1
123 777 28.1
125 787 19.7
128 999 19.5
129 781 18.2
124 778 15.6
ID_A:126 不匹配,因为 ID_B (762),match_score 对于另一个 ID_A (127) 更高。
ID_B:799 不匹配,因为 ID_A(125) 有一个更大的 match_score 和 (787)
如有任何帮助,我们将不胜感激!
我在 SAS 中找到了问题的解决方案,但是我在转换为 R 时遇到困难。
proc sort data=one;
by descending match_score ID_A ID_B;
run;
data want;
if _n_=1 then do;
dcl hash ha();
ha.definekey('ID_A');
ha.definedone();
dcl hash hb();
hb.definekey('ID_B');
hb.definedone();
end;
set one;
if ha.check()*hb.check() then do;
output;
ha.add();
hb.add();
end;
run;
我试图遵循您的逻辑。即使下面的代码看起来有点乱,我认为这是使用 base R
.
map_A <- data[duplicated(data$ID_A),]$ID_A
for(i in map_A) {
temp <- data[data$ID_A== i,]
index <- row.names(temp[which.min(temp$Match_score),])
data <- data[row.names(data)!= index,]
}
map_B <-data[duplicated(data$ID_B),]$ID_B
for(i in map_B) {
temp <- data[data$ID_B== i,]
index <- row.names(temp[which.min(temp$Match_score),])
data <- data[row.names(data)!= index,]
}
data[order(-data$Match_score),]
给予,
ID_A ID_B Match_score
127 762 55.1
123 777 28.1
125 787 19.7
128 999 19.5
129 781 18.2
124 778 15.6