1

Reproducible Example

Table1

df <- data.frame(Name = c('hisas', 'myjafs', 'namegaefi'))
       Name
1     hisas
2    myjafs
3 namegaefi

Table2

ref <- data.frame(Name = c('john', 'hello', 'his', 'name', 'random'))

    Name
1   john
2  hello
3    his
4   name
5 random

Expected Output

       Name   Test
1     hisas    T
2    myjafs    F
3 namegaefi    T

Question

I'm trying to use grepl to see if the string in table 2 can be found in table 1, and conditionally update a new column i. Test to reflect T or F

Failed Code

grepl(df$Name,ref$Name,  fixed=TRUE)
Javier
  • 730
  • 4
  • 17
  • 1
    Relevant [grepl in R to find matches to any of a list of character strings](https://stackoverflow.com/questions/25391975/grepl-in-r-to-find-matches-to-any-of-a-list-of-character-strings) – markus Jul 26 '19 at 06:02
  • 1
    `df$Test <- grepl(paste0(ref$Name, collapse = "|"), df$Name)` – Ronak Shah Jul 26 '19 at 06:02
  • ah i forgot abt the paste0 with pipe operator. Thanks so much! – Javier Jul 26 '19 at 06:03

3 Answers3

3

Just for fun a solution using adist. Of course, this won't scale well.

d <- adist(df$Name, ref$Name, costs = list(insertions = 1, deletions = 0, substitutions = 1))
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    3    4    0    3    5
#[2,]    3    5    2    3    5
#[3,]    3    4    2    0    4

rowSums(!d) > 0
#[1]  TRUE FALSE  TRUE
Roland
  • 127,288
  • 10
  • 191
  • 288
2

You could paste the ref$Name together as one pattern and then use grepl

df$Test <- grepl(paste0(ref$Name, collapse = "|"), df$Name)

df
#       Name  Test
#1     hisas  TRUE
#2    myjafs FALSE
#3 namegaefi  TRUE
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

You can do:

df <- data.frame(Name = c('hisas', 'myjafs', 'namegaefi'))
ref <- data.frame(Name = c('john', 'hello', 'his', 'name', 'random'))
sapply(df$Name, function(s) any(sapply(ref$Name, grepl,  x=s, fixed=TRUE)))
jogo
  • 12,469
  • 11
  • 37
  • 42