我正在尝试舍入(到当天)日期时间数据。这是数据的样子:
> date[10:15]
[1] "2016-08-31 23:03:02 EDT" "2016-08-31 22:52:50 EDT"
[3] "2016-08-31 22:50:02 EDT" "2016-08-31 22:34:28 EDT"
[5] "2016-08-31 22:31:41 EDT" "2016-08-31 22:25:37 EDT"
问题是,当我使用lubridate
函数round_date()
将日期“舍入”到年,月,日部分时,日期似乎已更改为UTC(注意:UTC比EDT早5个小时,所以00:40:17 EDT
是05:40:17 UTC
,如果我理解正确的话:
> date_r
[1] "2016-09-01 EDT" "2016-09-01 EDT" "2016-09-01 EDT"
[4] "2016-09-01 EDT" "2016-09-01 EDT" "2016-09-01 EDT"
这是一个代表:
structure(c(1472698982, 1472698370, 1472698202, 1472697268, 1472697101,
1472696737), class = c("POSIXct", "POSIXt"), tzone = "America/Detroit")
date_r <- round_date(date, unit = "day")
如果日期在EDT中包含8-31
,我正在尝试将舍入日期设为8-31
(不是9-1
)。如何在舍入日期时保留时区?打开非Lubridate
个答案。
答案 0 :(得分:1)
您可以在基础R中使用strptime
d = structure(c(1472698982, 1472698370, 1472698202, 1472697268, 1472697101,
1472696737), class = c("POSIXct", "POSIXt"), tzone = "America/Detroit")
strptime(d, format = "%Y-%m-%d", tz = "America/Detroit")
#OR
#strptime(d, format = "%Y-%m-%d", tz = attr(d, "tzone"))
#[1] "2016-08-31 EDT" "2016-08-31 EDT" "2016-08-31 EDT"
#[4] "2016-08-31 EDT" "2016-08-31 EDT" "2016-08-31 EDT"
或使用floor_date
lubridate
library(lubridate)
floor_date(d, unit = "day")
#[1] "2016-08-31 EDT" "2016-08-31 EDT" "2016-08-31 EDT"
#[4] "2016-08-31 EDT" "2016-08-31 EDT" "2016-08-31 EDT"