如何从POSIXct元素中提取正确的日期? [英] How to extract correct date from POSIXct element?

查看:302
本文介绍了如何从POSIXct元素中提取正确的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I get the correct date from the first column in my code?

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()不是正确的功能,我该如何选择日期(没有小时,分钟,秒)?

The first two dates are not correct. What did I do wrong?
If as.Date() is not the right function, how could I get the date (without hours, minutes, seconds) alternatively?

推荐答案

问题是时区

尝试您的时区(可能不是格林尼治标准时间)

try your timezone (probably not GMT)

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

并阅读帖子

这篇关于如何从POSIXct元素中提取正确的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆