This is very similar to this question, but with an added layer. I am looking to see if a string in one column exists in another column. But since for some rows the column is empty, when I run the code below I get a lot of 'TRUE' because they just match spaces. How can I ignore spaces and just match on characters?
word <- c('Hello','','nyc', '')
keywords <- c('hello goodbye nyc','hello goodbye nyc', 'hello goodbye nyc', 'hello goodbye nyc')
df <- data.frame(word, keywords, stringsAsFactors=F)
What I want is to add a new column (word_exists) that tells me if strings in column 'word' exists among 'keywords'. I tried:
df$word_exists <- mapply(grepl, pattern=df$keywords, x=df$word)
But get all 'TRUE' and I think it is because it is recognizing empty spaces in 'keywords' and matching them to empty 'words'. Any suggestions? Thanks!