r - 使用 dplyr 过滤 postgreSQL 数据库中的多个值

标签 r postgresql dplyr

我想过滤一个 postgres 数据库,这样我就可以将数据的一个子集导入 R 中进行分析。我可以通过单个条件(选择单个 featureid)成功过滤,但不能通过值向量过滤。例如,如果我这样设置与数据库的连接

library(dplyr)
db <- src_postgres(dbname = 'conte_dev', host = '155.0.0.x', port = '1234', user = '...', password = '...')
tbl_daymet <- tbl(db, 'daymet')

然后如果我过滤为单个值,它就可以工作

  tbl_filtered <- tbl_daymet %>%
    dplyr::filter(featureid == 739554)

tbl_filtered

Source: postgres 9.3.5 [conte@127.0.0.1:5432/conte_dev]
From: daymet [12,410 x 9]
Filter: featureid == 739554 

   featureid       date     tmax     tmin  prcp    dayl  srad  vp   swe
1     739554 1980-01-18  -1.9375 -12.2500 0.000 32140.8 199.6 240 100.5
2     739554 1980-01-19   1.1250  -3.4375 0.000 32140.8 100.4 480  99.0
3     739554 1980-01-20   0.0000  -7.5000 0.000 32486.4 160.4 360  99.0
4     739554 1980-01-21  -6.5000 -15.7500 0.000 32486.4 193.6 180  99.0
5     739554 1980-01-22 -11.8125 -18.7500 0.000 32486.4 156.8 140  99.0
6     739554 1980-01-23  -6.4375 -16.5000 3.000 32832.0 157.2 160 102.5
7     739554 1980-01-24  -6.8750 -19.0000 3.125 32832.0 178.0 120 105.0
8     739554 1980-01-25 -15.0000 -23.0625 0.000 32918.4 184.4  80 105.0
9     739554 1980-01-26  -9.9375 -20.7500 0.000 33177.6 229.2 120 105.0
10    739554 1980-01-27  -7.0625 -15.9375 0.000 33177.6 202.4 165 105.0
..       ...        ...      ...      ...   ...     ...   ... ...   ...

但是,如果我尝试过滤到 featureid 中的一组值

catches <- c(739554, 739554)
tbl_derived_metrics <- tbl_daymet %>%
    dplyr::filter(featureid %in% catches)

我得到一个错误

Error in postgresqlExecStatement(conn, statement, ...) : RS-DBI driver: (could not Retrieve the result : ERROR: syntax error at or near "739554" LINE 3: WHERE "featureid" IN 739554 ^ ) In addition: Warning message: In postgresqlQuickSQL(conn, statement, ...) : Could not create executeSELECT count(*) FROM (SELECT "featureid", "date", "tmax", "tmin", "prcp", "dayl", "srad", "vp", "swe" FROM "daymet" WHERE "featureid" IN 739554) AS "master"

我相信如果它是 R 中的数据框而不是 postgres 中的链接表,这会起作用。但是,我需要先进行过滤,因为该表包含数十亿行。有没有我可以使用的与 postgres 相关的特殊命令?无论我使用字符还是整数,当前代码都不起作用。

最佳答案

如果它们只是单个值而不是具有多个值的向量,则在 filter 函数中使用 %in% 不起作用。

对于具有 1 个或多个值的情况,它作为一个带有 ifelse 语句的函数。

retreiveDaymet <- function(catchmentid, num.catch) {
  catches <- catchmentid[1:num.catch]
  if(num.catch == 1) {
    tbl_derived_metrics <- tbl_daymet %>%
      dplyr::filter(featureid == catches)
  } else {
    tbl_derived_metrics <- tbl_daymet %>%
    dplyr::filter(featureid %in% catches)
  }

  derived_metrics <- collect(tbl_derived_metrics)
  return(derived_metrics)
}

然后可以这样使用

catchment.numbers <- rep(c(1, 10, 50, 100, 200, 400, 800, 1000, 1500, 2000, 2500, 3000), each = 3)
daymet.times <- data.frame(matrix(NA, length(catchment.numbers), 4))
for(i in 1:length(catchment.numbers)) {
  time1 <- system.time(foo <- retreiveDaymet(catchmentid = catchmentid, num.catch = catchment.numbers[i]))
  daymet.times[i, ] <- c(catchment.numbers[i], time1[1:3])
  rm(foo)
  rm(time1)
  gc(verbose = FALSE)
}
names(daymet.times) <- c("num.catchments", names(system.time(1+1))[1:3])

这个例子有点傻,因为 foo 每次都被丢弃。这仅用于计时目的。将来这段代码可以添加一个函数来每次对 foo 执行某些操作并将其附加到数据框或列表。

关于r - 使用 dplyr 过滤 postgreSQL 数据库中的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27789498/

相关文章:

postgresql - 在azure容器实例上部署Postgres数据库?

ruby - 为什么我的 Heroku 应用程序的 PostgreSQL 数据库上有打开的连接?我如何关闭它们?

r - 如何使用多个列执行左连接,其中一个数据框在一列中有缺失?

r - 删除时间重叠的行的有效方法

r - 使用 dplyr 计算时间戳上的累积和

r - 将数据框整理为 R 中的矩阵

r - 如何在R中向scatterplot3d添加图例

舍入所有数字列,但表中只有一个

ruby-on-rails - Rails 和模型 def 而不是列

r - 如何在 R 中使用 persp() 绘制数据集中的三个变量