I am trying to do a filter in dplyr where a column is like certain observations. I can use sqldf as
Test <- sqldf("select * from database
Where SOURCE LIKE '%ALPHA%'
OR SOURCE LIKE '%BETA%'
OR SOURCE LIKE '%GAMMA%'")
I tried to use the following which doesn't return any results:
database %>% dplyr::filter(SOURCE %like% c('%ALPHA%', '%BETA%', '%GAMMA%'))
Thanks
You can use
grepl
withALPHA|BETA|GAMMA
, which will match if any of the three patterns is contained in SOURCE column.If you want it to be case insensitive, add
ignore.case = T
ingrepl
.%like%
is from thedata.table
package. You're probably also seeing this warning message:The
%like%
operator is just a wrapper around thegrepl
function, which does string matching using regular expressions. So%
aren't necessary, and in fact they represent literal percent signs.You can only supply one pattern to match at a time, so either combine them using the regex
'ALPHA|BETA|GAMMA'
(as Psidom suggests) or break the tests into three statements: