dplyr filter() 带有类似 SQL 的 %wildcard% [英] dplyr filter() with SQL-like %wildcard%

查看:15
本文介绍了dplyr filter() 带有类似 SQL 的 %wildcard%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据:

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

为什么下面的代码返回0个结果(而不是第二行和第四行)?

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%")

我正在尝试对 dplyr::filter 的特定输入字符串使用 SQL 等效通配符过滤器,使用 %like% 运算符从 >data.table 包.

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.

我做错了什么?

推荐答案

您可以使用:

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

输出:

  Company    Metric
1     foo  9.906805
2    food 10.464493

正如 Dhawal Kapil 指出的,我认为 %like% 来自 data.table:

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

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

输出:

   Company    Metric
1:     foo  9.906805
2:    food 10.464493

这篇关于dplyr filter() 带有类似 SQL 的 %wildcard%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆