将UNIX TIMESTAMP转换为数据库查询dplyr中的日期
美好的一天,所有:
我有一个问题,涉及在dplyr命令中使用橄榄酸(或基本r)来编写数据库查询,将Unix时间戳(存储为整数)变成日期,然后从该日期变量提取一天。我要这样做的原因是,有一个可以轻松提取时间单元(y,q,m,d等)的日期,将允许进行一些分组和摘要,以减少查询数据所需的时间使用collect()。
在本地会话中,我可以从Unix Timestamp获得日期和时间,很容易地
df$timestamp <- as_datetime(df$timestamp)
df$Day <- format(as.Date(df$timestamp, format="%Y/%m/%d"),"%d")
尝试使用Dplyr语法将这种事情直接包含在RPOSTGRES数据库查询中。
在常用(和工作数据库连接的内容)之后,我尝试:
%>%
mutate(date = lubridate::as_datetime(timestamp, origin='1970-01-01', tz = "GMT"))%>%
select(date)
从此我得到错误,“ AS_DATETIME中的错误(TIMESTAMP,ORIGIN,ORIGIN =“ 1970-01-01”,TZ =“ GMT”): 未使用的参数(Origin =“ 1970-01-01”,TZ =“ GMT”)“
但是,当我尝试此操作时:
%>%
mutate(date = lubridate::as_datetime(timestamp))%>%
select(date)
我会得到此错误:无法准备查询:错误:不能在没有时区1行的情况下将Integer施加到时间戳上:选择Cast(“时间戳”作为时间戳)为“日期”,
谢谢,大家!
Good day, all:
I have a problem involving the use of lubridate (or base R) within a dplyr command to write a database query turning a Unix timestamp (stored as an integer) into a date and then extracting the day from that date variable. The reason I want to do this is that having a date from which I can easily extract time units (Y, Q, M, D, etc.) would allow for some grouping and summary that will reduce the time it takes to query the data using collect().
Within a local session, I can get date and time from a unix timestamp this easily enough
df$timestamp <- as_datetime(df$timestamp)
df$Day <- format(as.Date(df$timestamp, format="%Y/%m/%d"),"%d")
What I am trying to do is to include this sort of thing directly into a Rpostgres database query using dplyr syntax.
After the usual (and working database connection stuff), I try:
%>%
mutate(date = lubridate::as_datetime(timestamp, origin='1970-01-01', tz = "GMT"))%>%
select(date)
From this I get the error, "Error in as_datetime(timestamp, origin = "1970-01-01", tz = "GMT") :
unused arguments (origin = "1970-01-01", tz = "GMT")"
However, when I try this:
%>%
mutate(date = lubridate::as_datetime(timestamp))%>%
select(date)
I get this Error: Failed to prepare query: ERROR: cannot cast type integer to timestamp without time zone LINE 1: SELECT CAST("timestamp" AS TIMESTAMP) AS "date"
Thanks, everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论