gpt4 book ai didi

r - 概率链接 R 后去重

转载 作者:行者123 更新时间:2023-12-02 02:43:07 24 4
gpt4 key购买 nike

我刚刚对两个数据集进行了概率关联。称为“数据”的输出数据集包含来自两个原始数据集的标识号,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 有多种组合。我只想选择要配对的顶部链接,然后将它们从选择过程中删除以进行进一步链接。理想的输出是...

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) 不匹配,另一个 ID_A (127) 的 match_score 更高。

ID_B:799 不匹配,因为 ID_A(125) 与 (787) 的 match_score 更大

如有任何帮助,我们将不胜感激!

我在 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

关于r - 概率链接 R 后去重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57895045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com