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
  • 732
  • 4
  • 15

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
  • 122,144
  • 10
  • 182
  • 276
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
  • 355,584
  • 18
  • 123
  • 178
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,306
  • 11
  • 34
  • 41