Regex 使用正则表达式筛选dplyr sqlite3连接

Regex 使用正则表达式筛选dplyr sqlite3连接,regex,r,sqlite,dplyr,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"),

我想使用正则表达式通过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
库(nycflights13)

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

dplyr知道如何将以下R函数转换为SQL:

  • 基本数学运算符:+,-,*,/,%%,^
  • 数学函数:abs、acos、acosh、asin、asinh、atan、atan2、atanh、天花板、cos、cosh、cot、coth、exp、地板、日志、log10、, 圆形,符号,罪恶,信条,方形,棕褐色,棕褐色
  • 逻辑比较:,=,%in%
  • 布尔运算:&,&&,|,| |!,异或
  • 基本聚合:平均值、总和、最小值、最大值、sd、var
然而,
dplyr
将保留任何它无法翻译的内容,并将其传递给sqlite。这就是错误的来源-
dplyr
无法翻译
grepl
,因此将其传递给引发错误的sqlite

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
如果您懂一点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
..   ...   ...   ...      ...       ...      ...       ...     ...     ...    ...    ...   ...      ...