Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr filter() with SQL-like %wildcard%

Tags:

r

dplyr

Suppose I have the following data:

foo <- data.frame(Company = c("company1", "foo", "test", "food"), Metric = rnorm(4, 10))

> foo
   Company    Metric
1 company1 10.539970
2      foo  9.487823
3     test  9.663994
4     food  9.499327

Why does the following code return 0 results (instead of the second and fourth rows)?

library(dplyr)
library(data.table)

foo %>% dplyr::filter(Company %like% "%foo%")

I'm trying to use the SQL-equivalent wildcard filter on a particular input string to dplyr::filter, using the %like% operator from the data.table package.

What am I doing wrong?

like image 635
Ray Avatar asked Sep 28 '15 18:09

Ray


People also ask

What is wildcard filtering?

A WildcardFilter is a filter that applies a wildcard to a particular property. The wildcard matcher uses the question-mark (?) character to represent a single wildcard character and the asterisk (*) to represent multiple wildcard characters.

Is Dplyr a filter?

Of course, dplyr has 'filter()' function to do such filtering, but there is even more.


2 Answers

You can use:

filter(foo, grepl("foo", Company, fixed = TRUE))

Output:

  Company    Metric
1     foo  9.906805
2    food 10.464493

As Dhawal Kapil pointed out I think %like% is from data.table:

library(data.table)
DT <- data.table(foo)
DT[Company %like% 'foo']

Output:

   Company    Metric
1:     foo  9.906805
2:    food 10.464493
like image 186
mpalanco Avatar answered Sep 23 '22 03:09

mpalanco


You can use with library(stringr)

library(dplyr)
library(stringr)
foo <- data.frame(Company = c("company1", "foo", "test", "food"), Metric = rnorm(4, 10))

foo %>% filter(str_detect(Company,"foo"))

as well as any other Regular expression

foo %>% filter(str_detect(Company,"^f")) 
like image 45
W. Roberto Parra Avatar answered Sep 19 '22 03:09

W. Roberto Parra