首先,如果我的格式不好,我很抱歉,这是我第一次发帖,(也是编程和R的新手)
我尝试在字符串变量上将两个数据帧合并在一起。我正在合并大学名称,它们可能不完全匹配,所以我希望使用模糊或近似字符串匹配函数进行合并。当我找到‘fuzzyjoin’包时,我很高兴。
来自cranR: stringdist_join:基于列的模糊字符串匹配连接两个表
stringdist_join(x, y, by = NULL, max_dist = 2, method = c("osa", "lv",
"dl", "hamming", "lcs", "qgram", "cosine", "jaccard", "jw","soundex"), mode = "inner", ignore_case = FALSE, distance_col = NULL, ...)我的代码:
stringdist_left_join(new, institutions, by = c("tm_9_undergradu" = "Institution.Name"))错误:
Error in dists[include] <- stringdist::stringdist(v1[include], v2[include], :
NAs are not allowed in subscripted assignments我知道在这些列中有一些NA,但我不确定如何删除它们,因为我也需要它们。据我所知,NA的其他连接和合并功能将被忽略。有没有人知道如何避免这个包的错误,或者以另一种方式对字符串进行近似连接。谢谢你的帮助。
发布于 2019-06-20 21:15:31
这个答案对我很有效,来自GitHub
步骤1:找出哪个Df拥有NAs
`which(is.na(df1))
which(is.na(df2))`步骤2:用其他东西替换NAs。df1[is.na(df1)] <- "empty_string"
步骤3:运行连接(收到错误时我正在使用的代码)
`test1 <- msa_table %>%
as_tibble() %>%
unlist() %>%
mutate(msa = sub("\\(.*)","", as.character(msa)) %>%
stringdist_full_join(msa_table, df1, by = 'msa', max_dist = 2)` 对我来说,结果不是有相同的错误,但我的表中仍然有NAs。
希望这能有所帮助!另外,需要明确的是:这个解决方案来自GitHub上的Anton Prokopyev '@prokopyev‘。
发布于 2020-03-20 00:28:43
试一试
`test1 <- msa_table %>%
as_tibble() %>%
unlist() %>%
mutate(msa = stringr::str_squish(msa)) %>%
stringdist_full_join(msa_table, df1, by = 'msa', max_dist = 2)` https://stackoverflow.com/questions/53109370
复制相似问题