相关疑难解决方法(0)

在数据库的dplyr过滤器函数中传递SQL函数

我正在使用dplyr自动SQL后端从数据库表中查询子表.例如

my_tbl <- tbl(my_db, "my_table")
Run Code Online (Sandbox Code Playgroud)

其中,my_table在数据库中的样子

batch_name    value
batch_A_1     1
batch_A_2     2
batch_A_2     3
batch_B_1     8
batch_B_2     9
...
Run Code Online (Sandbox Code Playgroud)

batch_A_#无论数字如何,我只想要数据.

如果我在SQL中写这个,我可以使用

select * where batch_name like 'batch_A_%'
Run Code Online (Sandbox Code Playgroud)

如果我是在R写入这一点,我可以用一些方法来得到这样的:grepl(),%in%str_detect()

# option 1
subtable <- my_tbl %>% select(batch_name, value) %>%
    filter(grepl('batch_A_', batch_name, fixed = T))
# option 2
subtable <- my_tbl %>% select(batch_name, value) %>%
    filter(str_detect(batch_name, 'batch_A_'))
Run Code Online (Sandbox Code Playgroud)

所有这些都给出了以下Postgres错误: HINT: No function matches the given name and argument types. You might need to …

sql postgresql r dplyr

2
推荐指数
1
解决办法
1816
查看次数

标签 统计

dplyr ×1

postgresql ×1

r ×1

sql ×1