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?
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.
Of course, dplyr has 'filter()' function to do such filtering, but there is even more.
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
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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With