0

I have a data frame with some full names and a vector with first names:

d <- data.frame(myName = c("Adam Smith","John Smith","John Clarks "))

first <- c("Adam","Mark","Jim")

I need to check if any value from "first" is contained in each row from "d", so the result should look like this:

Adam Smith  TRUE
John Smith  FALSE
John Clarks FALSE

Could anyone help with this?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Andrei
  • 31
  • 5

1 Answers1

2

You can use grepl():

d <- data.frame(full_names = c("Adam Smith","John Smith","John Clarks "))

first <- c("Adam","Mark","Jim")

d$check <- grepl(paste0(first, collapse = "|"), d$full_names)

d
    full_names check
1   Adam Smith  TRUE
2   John Smith FALSE
3 John Clarks  FALSE
ottlngr
  • 1,137
  • 7
  • 13