I have the following data frame:
dat <- data.frame(Name = c("John", "Company A pty Ltd", ""), Surname = c("Smith", "", "Company B"), Company = c("Company D", "A Ltd", "Company B")) I want to check if the Company column contains any word that is either in the firstName or the Surname.
I have used the following code:
dat$clinicOnly <- mapply(grepl, pattern=dat$firstName, dat$Company) But what it checks is the whole string is present. Hence it works for the first row, but misses the second row, and gets the last row correct because it detected a blank firstName entry.
How to write a function that produces FALSE, TRUE, TRUE ?
apply(dat, 1, function(x){grepl(paste(x[1:2], collapse="|"), x[3])})