You can use grepl
function :
pattern <- 'apple'
filter <- grepl(pattern, ignore.case=T, mydf$subj.1) |
grepl(pattern, ignore.case=T, mydf$subj.2) |
grepl(pattern, ignore.case=T, mydf$subj.3)
subDF <- mydf[filter,]
> subDF
doc subj.1 subj.2 subj.3
1 blabla1 apple coconut berry
2 blabla2 prune apple banana
3 blabla3 coconut cherry apple and berry
EDIT :
About your question on for-loop, I don't see any problem in using it, and I doubt using a apply-family function would give many benefits in term of execution time.
For the error, the problem is that the string pattern passed to grepl
has to be a valid regular expression but '+'
is a special character and so '++'
is not allowed.
Anyway, if you just want to check if the subject string is contained in the column, you can disable the regular expression engine by setting the grepl
argument fixed=TRUE
(
this means pattern is a string to be matched as is).
The only drawback is that ignore.case
cannot be used with fixed = TRUE
.