Words <- c("corn", "barn")
Results <- sapply(Words, function(x) sum(grepl(x, DF$Things)))
Results
corn barn
3 1
I can't say why the stringr version of my code is not working for you. Can you make a small example of it not working, similar to what I put in my first post?
The str_detect version is returning NA because one of the values in the Things column is NA. The grepl function seems to ignore NA values. If you set the na.rm argument of sum() to TRUE, you will get the desired result.
To make a data frame of the counts, you culd use the data.frame function, though there is no reason you cannot store the vector that the original code produced.
Words <- c("corn", "barn")
Results <- sapply(Words, function(x) sum(grepl(x, DF$Things)))
Results <- data.frame(Words, Results)
Results
Words Results
corn corn 3
barn barn 1
If Results is large enough to make that unwieldy, you can post the output of
dput(head(Results, 20))
I would also change the name of the y column so it doesn't match the data frame name. That shouldn't be a problem but it strikes me as dangerous, though I did it myself.
It's late here, so I will not be able to respond for several hours. Someone else will, I hope.