dplyr中的异常日期时间行为
我正在尝试使用dplyr中的异常日期时间行为,r,dplyr,R,Dplyr,我正在尝试使用as.Date()生成UTC时区中记录的时间戳。这有时会在分组的tbl_df对象中产生无法解释的NA,但如果我将同一对象包含在data.frame(),解组(),或对其进行过滤,则不会产生。我的例子如下。分组的tbl_df对象是checkit,错误观察是#3,对于wcid=148。它的时间戳没有什么异常,但是as.Date()将为它返回一个NA,除非我如上所述转换checkit: > checkit Source: local data frame [6 x 3] Group
as.Date()
生成UTC时区中记录的时间戳。这有时会在分组的tbl_df对象中产生无法解释的NA,但如果我将同一对象包含在data.frame()
,解组()
,或对其进行过滤,则不会产生。我的例子如下。分组的tbl_df对象是checkit
,错误观察是#3,对于wcid=148。它的时间戳没有什么异常,但是as.Date()
将为它返回一个NA
,除非我如上所述转换checkit
:
> checkit
Source: local data frame [6 x 3]
Groups: wcid, ab_split_test [6]
wcid ab_split_test mailing_timestamp
(dbl) (chr) (time)
1 1 N <NA>
2 78 Y 2016-04-04 12:28:58
3 148 Y 2016-03-17 09:11:31
4 204 Y 2016-03-04 09:01:15
5 255 Y 2016-03-03 09:18:43
6 267 Y 2016-03-23 09:16:50
> class(checkit)
[1] "grouped_df" "tbl_df" "tbl" "data.frame"
> checkit %>% mutate(treatment_day_actual = as.Date(mailing_timestamp))
Source: local data frame [6 x 4]
Groups: wcid, ab_split_test [6]
wcid ab_split_test mailing_timestamp treatment_day_actual
(dbl) (chr) (time) (date)
1 1 N <NA> <NA>
2 78 Y 2016-04-04 12:28:58 2016-04-04
3 148 Y 2016-03-17 09:11:31 <NA>
4 204 Y 2016-03-04 09:01:15 2016-03-04
5 255 Y 2016-03-03 09:18:43 2016-03-03
6 267 Y 2016-03-23 09:16:50 2016-03-23
> ungroup(checkit) %>% mutate(treatment_day_actual = as.Date(mailing_timestamp))
Source: local data frame [6 x 4]
wcid ab_split_test mailing_timestamp treatment_day_actual
(dbl) (chr) (time) (date)
1 1 N <NA> <NA>
2 78 Y 2016-04-04 12:28:58 2016-04-04
3 148 Y 2016-03-17 09:11:31 2016-03-17
4 204 Y 2016-03-04 09:01:15 2016-03-04
5 255 Y 2016-03-03 09:18:43 2016-03-03
6 267 Y 2016-03-23 09:16:50 2016-03-23
> data.frame(checkit) %>% mutate(treatment_day_actual = as.Date(mailing_timestamp))
wcid ab_split_test mailing_timestamp treatment_day_actual
1 1 N <NA> <NA>
2 78 Y 2016-04-04 12:28:58 2016-04-04
3 148 Y 2016-03-17 09:11:31 2016-03-17
4 204 Y 2016-03-04 09:01:15 2016-03-04
5 255 Y 2016-03-03 09:18:43 2016-03-03
6 267 Y 2016-03-23 09:16:50 2016-03-23
> filter(checkit, wcid == 148) %>% mutate(treatment_day_actual = as.Date(mailing_timestamp))
Source: local data frame [1 x 4]
Groups: wcid, ab_split_test [1]
wcid ab_split_test mailing_timestamp treatment_day_actual
(dbl) (chr) (time) (date)
1 148 Y 2016-03-17 09:11:31 2016-03-17
我刚从dput()
中注意到时区缺失。当我查询它时,它显示为我的区域设置:
> attr(as.POSIXlt(checkit$mailing_timestamp),'tzone')
[1] "" "EST" "EDT"
这也不是应该的,因为my
dplyr::tbl()
调用中的sql参数专门请求UTC,如在中选择时区“UTC”处的邮寄时间戳作为邮寄时间戳。我正在连接PostgreSQL数据库。我无法使用dplyr_0.4.3.9001复制。我知道dplyr 0.4.3中的mutate
和组以及NA
存在一些问题,这些问题已在中修复。您能否使用dput
(即dput(checkit)
)提供几行数据。我对使用dplyr
的开发版本感到紧张。在运行collect(foo)
之前,我曾经能够检查SQL查询foo
是否返回了一个条件为nrow(compute(foo))==0的空对象。这个特性在开发版本中似乎被破坏了。我怀疑它会永远消失,所以现在我只想ungroup()
以获得正确的as.Date()
响应,并保持其他代码正常工作。
> attr(as.POSIXlt(checkit$mailing_timestamp),'tzone')
[1] "" "EST" "EDT"