【问题标题】:Cast timestamps to timestamptz using mutate_if使用 mutate_if 将时间戳转换为 timestamptz
【发布时间】:2018-11-02 05:22:46
【问题描述】:

我有一个带有时间戳(无时区)的数据库,我想在使用 collect() 之前将其转换为 timestamptz(带有时区)。到目前为止,我已经尝试了这些:

db_tbl %>% mutate_if(lubridate::is.timepoint, funs(CAST))

但是,我不知道如何在这个函数调用中添加AS timestamptz

它给了我:

Error in result_create(conn@ptr, statement) : 
  Failed to prepare query: ERROR:  syntax error at or near ")"
LINE 1: SELECT "user_id", "time_zone", CAST("confirmed_at") AS "conf...

db_tbl %>% mutate_if(lubridate::is.timepoint, funs(sql(paste0(., "::timestamptz"))))

但是,我不知道如何让paste0() 执行而不是被翻译成CONCAT: 它给了我:

Applying predicate on the first 100 rows
<SQL>
SELECT "user_id", "time_zone", CONCAT_WS('', "confirmed_at", '::timestamptz') AS "confirmed_at"
FROM (SELECT "user_id", "time_zone", "confirmed_at"
FROM app.app_users) "paiaayosfl"

最终,我试图在没有 R 的情况下使用我的本地时区来提取时间戳。

【问题讨论】:

    标签: r dplyr timestamp dbplyr


    【解决方案1】:

    为了我们未来的后代:

      .data %>%
        mutate_if(lubridate::is.timepoint, funs(CAST(. %as% timestamptz))) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 2015-04-20
      • 1970-01-01
      • 2013-04-07
      相关资源
      最近更新 更多