6

如何从代码的第一列中获取正确的日期?

test <- data.frame(posixdate = c("2013-05-01 00:59:00", "2013-05-01 01:59:00", "2013-05-01 02:59:00", "2013-05-01 03:59:00"))
test$posixdate <- as.POSIXct(test$posixdate, format="%Y-%m-%d %H:%M:%S" )
test$date <- as.Date(test$posixdate)

上面的代码导致:

  posixdate           date
1 2013-05-01 00:59:00 2013-04-30
2 2013-05-01 01:59:00 2013-04-30
3 2013-05-01 02:59:00 2013-05-01
4 2013-05-01 03:59:00 2013-05-01

前两个日期不正确。我做错了什么?
如果as.Date()不是正确的功能,我怎么能得到日期(没有小时、分钟、秒)?

4

2 回答 2

4

问题是时区

试试你的时区(可能不是格林威治标准时间)

test$date2 <- as.Date(test$posixdate, "GMT")

并阅读这篇文章

于 2015-03-23T09:40:32.183 回答
1

lubridate用包试试。这个对我有用。

library(lubridate)

as.Date(ymd_hms(test$posixdate))

[1] "2013-05-01" "2013-05-01" "2013-05-01" "2013-05-01"
于 2015-03-23T09:38:06.657 回答