0

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.

Data Example:

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

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.

Thanks -The newbie

lmcshane
  • 1,074
  • 4
  • 14
  • 27

1 Answers1

2

Here is something to start with

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
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • might another approach be to split email address at the @ symbol and use an ngram (perhaps 3 to cover most situations) to match? Longest common substrings (method = "lcs" doesn't deal with transpositions, as does method = "dl". I just read Mark van der Loo's article so I am grateful to test out some ideas. – lawyeR Aug 07 '14 at 00:52
  • Yep, sounds like an interesting attempt for another answer to me. :) – lukeA Aug 07 '14 at 07:08