使用mutate_if

时间:2018-11-02 05:22:46

标签: r dplyr timestamp dbplyr

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

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假定本地时区的情况下获取时间戳,因此,我也希望能为该更大的问题提供任何解决方案。谢谢!

1 个答案:

答案 0 :(得分:0)

谢谢你不要混蛋! (开玩笑)。对于我们未来的后代:

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