我试图通过模糊匹配在我的数据集的名称和位置中查找数据输入错误。我有一个来自原始数据的唯一键 siterow_id,并创建了一个新键 pi_key,我已经在其中确定了一些硬匹配项。(没有模糊匹配)。运行模糊匹配后,我得到重复值。某些 siterow_id 的连接左侧和右侧的匹配项。我可以手动查看数据并查看发生这种情况的位置以及删除行的硬代码。当我转到具有更多匹配项的更大数据集时,我想要一种更算法的方法。
我尝试这样做,但它删除了左侧和右侧的匹配项。如果可能的话,我会喜欢一个 tidyverse 的方式来做到这一点,而不是一个循环。
表格输出包括在下面。您可以在第 8 行和第 9 行看到重复项。
for(site in three_letter_matches$siterow_id.x){
if (any(three_letter_matches$siterow_id.y == site)) {
three_letter_matches <- three_letter_matches[!three_letter_matches$siterow_id.y == site,]
}
}
pi_key.x siterow_id.x last_name.x first_name.x city.x country.x pi_key.y siterow_id.y
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 6309 1-9CH29M kim kevin san f~ united s~ 11870 1-HC3YY6
2 7198 1-CJGRSZ kim jinseok seoul korea re~ 2952 1-2QBRZ2
3 7198 1-CJGRSZ kim jinseok seoul korea re~ 2952 1-3AHHSU
4 7198 1-CJGRSZ kim jinseok seoul korea re~ 2952 1-3JYF8V
5 7567 1-CW4DXI bar jair ramat~ israel 8822 1-E3UILG
6 8822 1-E3UILG bar jair ramat~ israel 7567 1-CW4DXI
7 11870 1-HC3YY6 kim kevin san f~ united s~ 6309 1-9CH29M
8 12357 1-HUUEA6 lee hyojin daeje~ korea re~ 13460 1-IGKCPP
9 13460 1-IGKCPP lee hyo jin daeje~ korea re~ 12357 1-HUUEA6
我找到了另一种方法
update <- three_letter_matches[!is.na(match(three_letter_matches$siterow_id.x, three_letter_matches$siterow_id.y)),]
update %<>% arrange(last_name.x, first_name.x) %>%
filter(row_number() %% 2 != 0)
three_letter_matches_update <- three_letter_matches %>%
anti_join(update)
仍然对建议持开放态度。