0

我有一个餐馆数据集,变量“CONAME”包含每个机构的名称。不幸的是,有很多拼写错误,我想更正它们。我已经使用以下代码尝试了 agrep 进行模糊集匹配(我将对所有主要链重复此代码):

rest2012$CONAME <- agrep("MC DONALD'S", rest2012$CONAME, ignore.case = FALSE, value = FALSE, max.distance = 3)

我收到以下错误消息: Error in $<-.data.frame( *tmp*, "CONAME", value = c(35L, 40L, 48L, : replacement has 3074 rows, data has 67424

有没有另一种方法可以替换拼写错误的名称,或者我只是使用错误的 agrep 函数?

4

1 回答 1

1

当您agrepvalue = FALSE结果一起使用时,“一个向量给出了产生匹配的元素的索引”。也就是说,匹配在您输入的名称向量中的位置agrep。然后,您尝试用较短的索引向量(其中 3074 行)替换数据框中的整个name变量(67424 行)。不是你想要的。这是一个小例子,也许可以指导您朝着正确的方向前进。你也可以阅读和这个。本身的细节(例如),我留给你。?Extractagrepmax.distance

# create a data frame with some MC DONALD's-ish names, and some other names.
rest2012 <- data.frame(CONAME = c("MC DONALD'S", "MCC DONALD'S", "SPSS Café", "GLM RONALDO'S", "MCMCglmm"))
rest2012

# do some fuzzy matching with 'agrep'
# store the indices in an object named 'idx'
idx <- agrep(pattern = "MC DONALD'S", x = rest2012$CONAME, ignore.case = FALSE, value = FALSE, max.distance = 3)
idx

# just look at the rows in the data frame that matched
# indexing with a numeric vector 
rest2012[idx, ]

# replace the elements that matches 
rest2012[idx, ] <- "MC DONALD'S"
rest2012
于 2013-10-10T19:12:24.573 回答