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/