1 votes

Select cells that have certain words

I have a data frame similar to this

 dt=data.frame(datos=c("pizza simple","pizza con jamon","pizza con queso","hamburguesa con queso",
                      "nachos con queso","papas","tacos","chalupa"))

and I have a vector with keywords


I want to see which cells have those words, something like this:

1          pizza simple   pizza
2       pizza con jamon   pizza
3       pizza con queso   pizza queso
4 hamburguesa con queso   queso
5      nachos con queso   queso
6                 papas
7                 tacos
8               chalupa

I have tried with this code

 dt %>% filter(str_detect(datos,claves))

However what I get is not what I'm looking for

1          pizza simple
2       pizza con queso
3 hamburguesa con queso


Patricio Moracho Points 24098

Your solution is adequate except for a small detail, the second parameter of str_detect expects a regular expression pattern and you are simply passing it a vector of strings. The appropriate pattern to use would be a or which is expressed as pizza|queso , so the solution could be:

 dt %>% 
   filter(str_detect(datos, paste0(claves, collapse="|")))

1          pizza simple
2       pizza con jamon
3       pizza con queso
4 hamburguesa con queso
5      nachos con queso


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: