使用any()使用dbplyr从SQLite数据库中获取数据
我想通过使用 any()
函数结合 group_by
来从 R 中的本地 SQLite 数据库中获取数据,以过滤至少一行等于某个值的组健康)状况。最终学习 SQL 可能会有所帮助,但是,到目前为止,我设法使用 dbplyr 完成所有查询,我希望也有一个 dplyr 解决方案来解决这个问题。
db <- dbConnect(RSQLite::SQLite(), "test_db.sqlite")
test_table <- tibble(id = c(rep(1:3, each = 3)),
cond = c(rep("A", 8), "B"))
dbWriteTable(db, "table", test_table)
table <- tbl(db, "table")
当表已经在内存中时,我可以轻松地完成我想要的操作
test_table %>%
group_by(id) %>%
filter(any(cond == "B"))
,这给了我
id cond
<int> <chr>
1 3 A
2 3 A
3 3 B
但是这不起作用:
table %>%
group_by(id) %>%
filter(any(cond == "B"))
它会导致以下错误:
error: no such function: any
是否有 dbplyr 解决方法?
I want to fetch data from a local SQLite database in R by using the any()
function in combination with group_by
to filter groups where at least one row is equal to a certain condition. It would probably help to finally learn SQL, however, until now I managed to do all my queries using dbplyr and I hope there is a dplyr solution for this problem as well.
db <- dbConnect(RSQLite::SQLite(), "test_db.sqlite")
test_table <- tibble(id = c(rep(1:3, each = 3)),
cond = c(rep("A", 8), "B"))
dbWriteTable(db, "table", test_table)
table <- tbl(db, "table")
With the table already in memory I can accomplish what I want easily using
test_table %>%
group_by(id) %>%
filter(any(cond == "B"))
which gives me
id cond
<int> <chr>
1 3 A
2 3 A
3 3 B
However this does not work:
table %>%
group_by(id) %>%
filter(any(cond == "B"))
It results in the following error:
error: no such function: any
Is there a dbplyr workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个有效的解决方案:
Here is a solution that works: