regex - 使用正则表达式过滤 dplyr sqlite3 连接

标签 regex r sqlite dplyr

我想使用正则表达式通过 dplyr 从 sqlite3 连接中选择行,不幸的是这似乎不可能。是否有解决方法可以根据正则表达式过滤这些行?下面的代码显示了错误。

library(nycflights13)
my_db <- src_sqlite("my_db.sqlite3", create = T)
flights_sqlite <- copy_to(my_db, flights, temporary = FALSE, indexes = list(c("year", "month", "day"), "carrier", "tailnum"))
flights_sqlite <- tbl(nycflights13_sqlite(), "flights")
filter(flights_sqlite, grepl("N9.*", tailnum))

#> Error in sqliteSendQuery(con, statement, bind.data) :
#> error in statement: no such function: GREPL

最佳答案

不幸的是,dplyr 无法将许多有用的函数转换为传递给 sqlite 的 SQL 查询。您可以在 dplyr database vignette 中查看它可以实现的功能列表。 :

dplyr knows how to convert the following R functions to SQL:

  • basic math operators: +, -, *, /, %%, ^
  • math functions: abs, acos, acosh, asin, asinh, atan, atan2, atanh, ceiling, cos, cosh, cot, coth, exp, floor, log, log10, round, sign, sin, sinh, sqrt, tan, tanh
  • logical comparisons: <, <=, !=, >=, >, ==, %in%
  • boolean operations: &, &&, |, ||, !, xor
  • basic aggregations: mean, sum, min, max, sd, var

但是,dplyr 将保留任何它无法翻译的内容并将其传递给 sqlite。这就是错误的来源 - dplyr 无法翻译 grepl,因此将其传递给 sqlite,从而引发错误。

如果您了解一点 SQL,您可以使用 %like% 编写自己的等效查询:

filter(flights_sqlite, tailnum %like% "N9%")

Source: sqlite 3.8.6 
From: flights [30,216 x 16]
Filter: tailnum %like% "N9%" 

    year month   day dep_time dep_delay arr_time arr_delay carrier tailnum flight origin  dest air_time
   (int) (int) (int)    (int)     (dbl)    (int)     (dbl)   (chr)   (chr)  (int)  (chr) (chr)    (dbl)
1   2013     1     1      602        -8      812        -8      DL  N971DL   1919    LGA   MSP      170
2   2013     1     1      608         8      807        32      MQ  N9EAMQ   3768    EWR   ORD      139
3   2013     1     1      655        -5     1002       -18      DL  N997DL   2003    LGA   MIA      161
4   2013     1     1      659        -6      907        -6      DL  N998DL    831    LGA   DTW      105
5   2013     1     1      717        -3      850        10      FL  N978AT    850    LGA   MKE      134
6   2013     1     1      754        -5     1039        -2      DL  N935DL   2047    LGA   ATL      126
7   2013     1     1      759        -1     1057       -30      DL  N955DL   1843    JFK   MIA      158
8   2013     1     1      804        -6     1103       -13      DL  N947DL   1959    JFK   MCO      147
9   2013     1     1      810         0     1048        11      9E  N915XJ   3538    JFK   MSP      189
10  2013     1     1      814         4     1047        17      FL  N977AT    346    LGA   ATL      132
..   ...   ...   ...      ...       ...      ...       ...     ...     ...    ...    ...   ...      ...

关于regex - 使用正则表达式过滤 dplyr sqlite3 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33878269/

相关文章:

android - 即使我清除数据并从 6.0.1 设备删除应用程序,Sqlite 数据库仍然存在

r - "Spread"na.locf数据为不规则时间数据

android - 插入时数据类型不匹配(代码 20)

javascript - 如何使用 Regex 将两个双引号替换为一个双引号而不捕获一个双引号的出现?

python - 识别字符是python中单词中的数字还是Unicode字符

r - ggplot2 中类似的 xlims 和 ylims

r - 如何使用 ggplot2 和刻度格式化带有指数的轴标签?

sql - 自定义“排序依据”,并包含一些偏好的行?

正则表达式限制 url 文件夹的通配符

c++ - 使用 RegEx 过滤错误的输入?