如何在 R 中执行近似(模糊)名称匹配

2024-05-19

我有一个专门用于生物学期刊的大型数据集,该数据集是由不同的人长时间编写的。因此,数据不采用单一格式。例如,在“作者”栏中我可以找到John Smith、Smith John、Smith J等,但它们是同一个人。我连最简单的动作都做不了。例如,我无法弄清楚哪些作者写的文章最多。

R中有没有办法判断不同名称中的大多数符号是否相同,将它们视为相同的元素?


有一些软件包可以帮助您解决此问题,其中一些在评论中列出。但是,如果你不想使用这些,我想我会尝试用 R 编写一些可能对你有帮助的东西。该代码会将“John Smith”与“J Smith”、“John Smith”、“Smith John”、“John S”相匹配。同时,它不会匹配“John Sally”之类的内容。

# generate some random names
names = c(
  "John Smith", 
  "Wigberht Ernust",
  "Samir Henning",
  "Everette Arron",
  "Erik Conor",
  "Smith J",
  "Smith John",
  "John S",
  "John Sally"
);

# split those names and get all ways to write that name
split_names = lapply(
  X = names,
  FUN = function(x){
    print(x);
    # split by a space
    c_split = unlist(x = strsplit(x = x, split = " "));
    # get both combinations of c_split to compensate for order
    c_splits = list(c_split, rev(x = c_split));
    # return c_splits
    c_splits;
  }
)

# suppose we're looking for John Smith
search_for = "John Smith";

# split it by " " and then find all ways to write that name
search_for_split = unlist(x = strsplit(x = x, split = " "));
search_for_split = list(search_for_split, rev(x = search_for_split));

# initialise a vector containing if search_for was matched in names
match_statuses = c();

# for each name that's been split
for(i in 1:length(x = names)){

  # the match status for the current name
  match_status = FALSE;

  # the current split name
  c_split_name = split_names[[i]];

  # for each element in search_for_split
  for(j in 1:length(x = search_for_split)){

    # the current combination of name
    c_search_for_split_names = search_for_split[[j]];

    # for each element in c_split_name
    for(k in 1:length(x = c_split_name)){

      # the current combination of current split name
      c_c_split_name = c_split_name[[k]];

      # if there's a match, or the length of grep (a pattern finding function is
      # greater than zero)
      if(
        # is c_search_for_split_names first element in c_c_split_name first
        # element
        length(
          x = grep(
            pattern = c_search_for_split_names[1],
            x = c_c_split_name[1]
          )
        ) > 0 &&
        # is c_search_for_split_names second element in c_c_split_name second 
        # element
        length(
          x = grep(
            pattern = c_search_for_split_names[2],
            x = c_c_split_name[2]
          )
        ) > 0 ||
        # or, is c_c_split_name first element in c_search_for_split_names first 
        # element
        length(
          x = grep(
            pattern = c_c_split_name[1],
            x = c_search_for_split_names[1]
          )
        ) > 0 &&
        # is c_c_split_name second element in c_search_for_split_names second 
        # element
        length(
          x = grep(
            pattern = c_c_split_name[2],
            x = c_search_for_split_names[2]
          )
        ) > 0
      ){
        # if this is the case, update match status to TRUE
        match_status = TRUE;
      } else {
        # otherwise, don't update match status
      }
    }
  }

  # append match_status to the match_statuses list
  match_statuses = c(match_statuses, match_status);
}

search_for;

[1] "John Smith"

cbind(names, match_statuses);

     names             match_statuses
[1,] "John Smith"      "TRUE"        
[2,] "Wigberht Ernust" "FALSE"       
[3,] "Samir Henning"   "FALSE"       
[4,] "Everette Arron"  "FALSE"       
[5,] "Erik Conor"      "FALSE"       
[6,] "Smith J"         "TRUE"        
[7,] "Smith John"      "TRUE"        
[8,] "John S"          "TRUE"
[9,] "John Sally"      "FALSE"   

希望这段代码可以作为一个起点,您可能希望调整它以处理任意长度的名称。

一些注意事项:

  • forR 中的循环可能很慢。如果您正在使用很多名称,请查看Rcpp.

  • 您可能希望将其包装在一个函数中。然后,您可以通过调整将其应用于不同的名称search_for.

  • 此示例存在时间复杂性问题,并且根据数据的大小,您可能想要/需要重新设计它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 R 中执行近似(模糊)名称匹配 的相关文章

  • 纵向序列数据的三次样条方法?

    我有一个串行数据 格式如下 time milk Animal ID 30 25 6 1 31 27 2 1 32 24 4 1 33 17 4 1 34 33 6 1 35 25 4 1 33 29 4 2 34 25 4 2 35 24
  • 使用 purrr 迭代替换数据帧列中的字符串

    我想用purrr使用以下命令在数据框列上迭代运行多个字符串替换gsub 功能 这是示例数据框 df lt data frame Year 2019 Text c rep a aa 5 rep a bb 3 rep a cc 2 gt df
  • 如何使用 R 计算成为列表中中位数的概率?

    假设我有以下数据集 其中显示了假设实验的每个状态的三个观察结果的列表 state lt c Iowa Minnesota Illinois outcome lt list c 5 11 11 c 3 12 8 c 9 14 2 dat lt
  • 将数据框中的每个 x 个字符拆分为字符串

    我知道这里有一些关于每隔一段时间分割一个字符串的答案nth字符 例如this one https stackoverflow com questions 23208490 split each character in r and this
  • R独特的列或行与NA无可比拟

    有谁知道如果incomparables的论证unique or duplicated 曾经被实施过incomparables FALSE 也许我不明白它应该如何工作 无论如何 我正在寻找一个巧妙的解决方案 以仅保留与另一列相同的唯一列 或行
  • API 请求和curl::curl_fetch_memory(url, handle = handle) 中的错误:SSL 证书问题:证书已过期

    几天前 我运行了代码几个月 没有任何问题 GET url myurl query 今天我遇到一个错误 Error in curl curl fetch memory url handle handle SSL certificate pro
  • Purrr::map_df() 删除 NULL 行

    使用时purrr map df 我偶尔会传递一个数据框列表 其中一些项目是NULL 当我做 map df 返回行数少于原始列表的数据框 我想发生的事情是这样的map df calls dplyr bind rows 它忽略了NULL价值观
  • 以引用透明的方式从函数的省略号参数中提取符号

    事情又发生了 我正要按下发布答案按钮的问题被删除了 我正在寻找一种方法来从函数的省略号参数中提取绑定到符号的对象的值以及符号 也就是说 我试图以引用透明的方式从省略号中提取符号 我尝试过使用替代品和lazy dots 但没有成功 funct
  • r 中训练和测试数据的最小最大缩放/归一化

    我正在创建一个函数 它将训练集和测试集作为其参数 最小 最大缩放 标准化并返回训练集并使用这些same最小值和最小 最大范围的值 标准化并返回测试集 到目前为止 这是我想出的功能 min max scaling lt function tr
  • 朴素贝叶斯分类器仅基于先验概率做出决策

    我试图根据推文的情绪将推文分为三类 买入 持有 卖出 我正在使用 R 和包 e1071 我有两个数据框 一个训练集和一组需要预测情绪的新推文 训练集数据框 text sentiment this stock is a good buy Bu
  • 将数据框中重叠的范围合并到唯一的组中

    我有一个 n 行 3 的数据框 df lt data frame start c 178 400 983 1932 33653 end c 5025 5025 5535 6918 38197 group c 1 1 2 2 3 df sta
  • 更改闪亮 R 中的默认浏览器

    我在 RStudio 中使用 01 hello 虽然在 IE 中默认打开程序时它不会显示直方图 但即使在 Chrome 中 滑块也不起作用 我无法滑动条形图并看到直方图中的变化 如何更改 R 中的默认浏览器 以便闪亮启动 Chrome 而不
  • 在 Rcpp 中使用其他包中的 C 函数

    我试图从 C 函数中的 cubature 包调用 C 例程来执行多维积分 我试图重现的基本 R 示例是 library cubature integrand lt function x sin x adaptIntegrate integr
  • 使用 ggmap 截断密度多边形

    我在使用 R ggmap 绘制密度图时遇到问题 我的数据如下所示 gt head W date lat lon dist 1 2010 01 01 31 942 86 659 292 415 2 2010 01 10 32 970 84 1
  • 纵向比较 R 中的值...并进行扭转

    我有许多人在多达四个时间段进行的测试结果 这是一个示例 dat lt structure list Participant ID c A A A A B B B B C C C C phase structure c 1L 2L 3L 4L
  • 将不均匀的层次列表转换为数据框

    我认为还没有有人问过这个问题 但是有没有一种方法可以将具有多个级别和不均匀结构的列表的信息组合成 长 格式的数据帧 具体来说 library XML library plyr xml inning lt http gd2 mlb com c
  • SPSS 中的标准化残差与 R rstandard(lm()) 不匹配

    在寻找 R 相关解决方案时 我发现 R 和 SPSS 版本 24 在计算简单线性模型中的标准化残差方面存在一些不一致 看来SPSS所谓的标准化残差匹配 R学生化残差 我完全不认为某处存在软件错误 但显然这两个程序之间存在差异 看看这个例子
  • 无法更改 RStudio 中的 R 版本

    我的 RStudio V 0 99 491 无法更改 R 版本 我以平常的方式行事Global Options gt R Version 然后它挂起并且不再工作或反应 R 运行良好的初始版本是R 3 1 0 我以前从未遇到过这样的问题 也许
  • 线性判别分析图

    如何将样本 ID 行号 作为标签添加到此 LDA 图中的每个点 library MASS ldaobject lt lda Species data iris plot ldaobject panel function x y points
  • 如何使用 dplyr 独立过滤每列的行

    我有以下内容 library tidyverse df lt tibble tribble gene colB colC a 1 2 b 2 3 c 3 4 d 1 1 df gt A tibble 4 x 3 gt gene colB c

随机推荐