find if string contains mutliple words in r -
i have following data frame:
dat <- data.frame(name = c("john", "company pty ltd", ""), surname = c("smith", "", "company b"), company = c("company d", "a ltd", "company b"))
i want check if company column contains word either in firstname or surname.
i have used following code:
dat$cliniconly <- mapply(grepl, pattern=dat$firstname, dat$company)
but checks whole string present. hence works first row, misses second row, , gets last row correct because detected blank firstname entry.
how write function produces false, true, true ?
how this, using intersect
hard work?:
v1 <- strsplit(do.call(paste, dat[1:2]), "\\s+") v2 <- strsplit(as.character(dat$company), "\\s+") mapply(function(x,y) length(intersect(x,y)) > 1, v1, v2) #[1] false true true
Comments
Post a Comment