R多重模糊匹配agrep创建变量 [英] R multiple fuzzy match agrep create variable

查看:93
本文介绍了R多重模糊匹配agrep创建变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R的新手.我想通过创建一个变量(是/否)来创建测试,以检查名字或姓氏是否与电子邮件地址模糊匹配.如果是这样,请在该行后面附加一个是"变量.

New to R. I would like to create a test by creating a variable (yes/no) that checks to see if first name OR last name fuzzy match to email address. If so, append a 'yes' variable to that row.

数据示例:

id firstname lastname email address match
1 patrick boyles patrickb@gmail.com yes
2 zeke cosmos zeke@gmail.com yes
3 foo foo abcd@gmail.com no

我了解我需要使用agrep.令我困惑的是如何告诉R检查2列(名字和姓氏)并且仅在该行中检查.

I understand that I need to use agrep. What confuses me is how to tell R to check 2 columns (first name and last name) and only check within that row.

谢谢 -新手

推荐答案

以下是一些开始

library(stringdist) # install.packages("stringdist") b4, if you need to
df <- read.table(header = TRUE, text = "id firstname lastname emailaddress match
1 patrick boyles patrickb@gmail.com yes
2 zeke cosmos zeke@gmail.com yes
3 foo foo abcd@gmail.com no")
df$match2 <- ifelse(with(df, stringdist(a = paste0(firstname, lastname), 
                                        b = sub("(.*)@.*", "\\1", emailaddress), 
                                        method = "lcs")) <= 7, 
                    "yes", "no")
df
#   id firstname lastname      email.address match match2
# 1  1   patrick   boyles patrickb@gmail.com   yes    yes
# 2  2      zeke   cosmos     zeke@gmail.com   yes    yes
# 3  3       foo      foo     abcd@gmail.com    no     no

这篇关于R多重模糊匹配agrep创建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆