r - dplyr 包 : How can I query large data frame using like '%xyz%' SQL syntax?

标签 r dplyr

dplyr 是唯一可以处理我的 843k data.frame 并以快速方式查询它的包。 我可以使用一些数学和相等标准进行精细过滤,但是我需要实现对概念的搜索。

我需要类似这样的 sqldf 查询

library(sqldf)
head(iris)
sqldf("select * from iris where lower(Species) like '%nica%'")

在 dplyr 帮助中,我找不到如何做到这一点。类似:

filter(iris,Species like '%something%')

开始和结束的 % 非常重要。另外,请注意数据框有 800+k 行,因此传统的 R 函数可能运行缓慢。它必须是一个基于 dplyr 的解决方案。

最佳答案

这个怎么样 -

library(dplyr)
data(iris)
filter(iris, grepl("nica",Species))

编辑:另一个选项 - data.table()

中的函数 %like%
library(dplyr)
data(iris)
##
Iris <- iris[
  rep(seq_len(nrow(iris)),each=5000),
  ]
dim(Iris)
[1] 750000      5
##
library(microbenchmark)
library(data.table)
##
Dt <- data.table(Iris)
setkeyv(Dt,cols="Species")
##
foo <- function(){
  subI <- filter(Iris, grepl("nica",Species))
}
##
foo2 <- function(){
  subI <- Dt[Species %like% "nica"]
}
##
foo3 <- function(){
  subI <- filter(Iris, Species %like% "nica")
}
Res <- microbenchmark(
  foo(),foo2(),foo3(),
  times=100L)
##
> Res
Unit: milliseconds
   expr       min        lq    median        uq      max neval
  foo() 114.31080 122.12303 131.15523 136.33254 214.0405   100
 foo2()  23.00508  30.33685  39.77843  41.49121 129.9125   100
 foo3()  18.84933  22.47958  29.39228  35.96649 114.4389   100

关于r - dplyr 包 : How can I query large data frame using like '%xyz%' SQL syntax?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24892529/

相关文章:

r - data.table 中圆括号前的点,.()

r - mutate 中的嵌套 ifelse 会产生错误的输出

r 使用 dplyr 'gather' 函数

r - 精确日期的左连接值,如果缺失则查找前一个值

对具有特定名称模式的几组列进行 Rowsum

r - 使用 dplyr 汇总多列 - 分类版本

r - 如何使用 dplyr 按组减去值(减去存储为一组的空白)?

r - 只有组中差异小于 'n' 的行

r - 使用ggplot2为每组添加回归线

R markdown - 包括带有使用 RSVGTipsDevice 包生成的工具提示的 svg 图像