【问题标题】:Comparing strings between dataframes比较数据帧之间的字符串
【发布时间】:2018-10-04 20:55:35
【问题描述】:

我希望了解 R 中函数或工具的可行性和任何建议,以循环遍历一个数据帧中的列,并逐个字符串比较另一个数据帧的列中的每个字符串。

这个想法是为每个字符串分配一个“是/否/也许”,以便在另一个数据框中寻找“可能的匹配”(可能存在拼写错误或输入错误......我只是想缩小范围搜索匹配)

这是 R 可以很好处理的事情吗?

【问题讨论】:

标签: r string dataframe comparison


【解决方案1】:

欢迎来到stackoverflow!有一个名为 adist 的函数计算两个字符串之间的 Levenshtien 编辑距离(参见类似问题 here)。

在不知道您的具体用例的情况下,我们可以举一个例子:

df <- data.frame(a = c('comparing', 'strings', 'between', 'dataframes'),
                 b = c('comparing', 'integers', 'between', 'data.frames'),
                 stringsAsFactors = F)

df$levenshtein <- mapply(adist, df$a, df$b)
df$ismatch <- 'maybe'
df$ismatch[df$levenshtein == 0] <- 'yes'
df$ismatch[df$levenshtein >= 3] <- 'no'

df
# gives:
           a           b levenshtein ismatch
1  comparing   comparing           0     yes
2    strings    integers           6      no
3    between     between           0     yes
4 dataframes data.frames           1   maybe

您当然可以更改'maybe' 答案的范围。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2023-04-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多