使用模糊匹配替换数据框列中的重复值
Replace duplicate values from dataframe column using fuzzy match
我正在尝试使用 library('RecordLinkage') 和 compare.dedup() 函数来替换单个列中的重复值。
与此类似,我有一个向量
tv3 = c("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDE FRANCE", "TOURDE FRANZ", "GET FRESH")
我想要的输出如下,基于权重的设定值(例如 > 0.8):
("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDEFRANCE", "TOURDEFRANCE", "GET FRESH")
这是我尝试获取匹配数据框的代码:
tv3 = as.data.frame(c("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDE FRANCE",
"TOURDE FRANZ", "GET FRESH"))
colnames(tv3) <- "name"
tv3 %>% compare.dedup(strcmp = TRUE) %>%
epiWeights() %>%
epiClassify(0.8) %>%
getPairs(show = "links", single.rows = TRUE) -> matches
但是为了得到我需要的东西,我使用了下面的循环:
matches <- matches[order(matches$id1),]
tv3new <- tv3
for (i in 1:nrow(matches)) {
tv3new[tv3new$name==matches[i,'name.2'],] <- matches[i,'name.1']
}
tv3new
这给了我想要的,但想知道使用循环是否是最好的方法,或者我是否遗漏了一些明显的东西。
没有循环:
tv3new <- c(as.character(matches[tv3$name %in% matches$name.2*1, 2]),
as.character(tv3[!tv3$name %in% matches$name.2, ]))
# If we need a data frame
data.frame(name = tv3new)
输出:
name
1 TOURDEFRANCE
2 TOURDEFRANCE
3 TOURDEFRANCE
4 TOURDEFRANCE
5 GET FRESH
我正在尝试使用 library('RecordLinkage') 和 compare.dedup() 函数来替换单个列中的重复值。
与此类似
tv3 = c("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDE FRANCE", "TOURDE FRANZ", "GET FRESH")
我想要的输出如下,基于权重的设定值(例如 > 0.8):
("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDEFRANCE", "TOURDEFRANCE", "GET FRESH")
这是我尝试获取匹配数据框的代码:
tv3 = as.data.frame(c("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDE FRANCE",
"TOURDE FRANZ", "GET FRESH"))
colnames(tv3) <- "name"
tv3 %>% compare.dedup(strcmp = TRUE) %>%
epiWeights() %>%
epiClassify(0.8) %>%
getPairs(show = "links", single.rows = TRUE) -> matches
但是为了得到我需要的东西,我使用了下面的循环:
matches <- matches[order(matches$id1),]
tv3new <- tv3
for (i in 1:nrow(matches)) {
tv3new[tv3new$name==matches[i,'name.2'],] <- matches[i,'name.1']
}
tv3new
这给了我想要的,但想知道使用循环是否是最好的方法,或者我是否遗漏了一些明显的东西。
没有循环:
tv3new <- c(as.character(matches[tv3$name %in% matches$name.2*1, 2]),
as.character(tv3[!tv3$name %in% matches$name.2, ]))
# If we need a data frame
data.frame(name = tv3new)
输出:
name
1 TOURDEFRANCE
2 TOURDEFRANCE
3 TOURDEFRANCE
4 TOURDEFRANCE
5 GET FRESH