栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过R中变量的模糊匹配进行合并

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过R中变量的模糊匹配进行合并

agrep
函数(基数R的一部分)使用Levenshtein编辑距离进行近似字符串匹配可能值得尝试。不知道您的数据是什么样子,我无法真正提出可行的解决方案。但这是一个建议……它会将比赛记录在一个单独的列表中(如果有多个同样出色的比赛,那么也会记录这些比赛)。假设您的data.frame称为
df

l <- vector('list',nrow(df))matches <- list(mother = l,father = l)for(i in 1:nrow(df)){  father_id <- with(df,which(student_name[i] == father_name))  if(length(father_id) == 1){    matches[['father']][[i]] <- father_id  } else {    old_father_id <- NULL    ## try to find the total for(m in 10:1){ ## m is the maximum distance     father_id <- with(df,agrep(student_name[i],father_name,max.dist = m))      if(length(father_id) == 1 || m == 1){        ## if we find a unique match or if we are in our last round, then stop          matches[['father']][[i]] <- father_id        break      } else if(length(father_id) == 0 && length(old_father_id) > 0) {        ## if we can't do better than multiple matches, then record them anyway         matches[['father']][[i]] <- old_father_id        break      } else if(length(father_id) == 0 && length(old_father_id) == 0) {        ## if the nearest match is more than 10 different from the current pattern, then stop      break      }    }  }}

的代码

mother_name
基本相同。您甚至可以将它们组合成一个循环,但是此示例仅出于说明目的。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386509.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号