将日期时间字符串转换为R中的POSIXct日期/时间格式 [英] converting datetime string to POSIXct date/time format in R

查看:765
本文介绍了将日期时间字符串转换为R中的POSIXct日期/时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个字符串格式

test <- "YYYY-MM-DDT00:00:00.000-08:00"

我的目标是将这些字符串转换为 POSIXct 格式,以便我可以绘制数据。我最初的想法是使用

My goal is to convert those strings to POSIXct format so that I can plot the data. my initial thought was to use

as.POSIXct(test)

...但似乎截断datetime到只是日期。有什么想法吗? as.POSIXct 的帮助信息似乎暗示输入应该是空格分隔的日期和时间,而不是T。这是我的问题吗?

...but that seems to truncate the datetime to just date. Any thoughts? The help info for as.POSIXct seems to imply that the input should be date and time separated by a space, not by a "T". Is this my issue?

推荐答案

您需要指定转换格式。阅读?strptime 以查看日期格式的所有选项。

You need to specify a format for your conversion. Take a read of ?strptime to see all of the options for date formats.

#YYYY-MM-DDT00:00:00.000-08:00
test <- "2013-12-25T04:32:16.500-08:00"
z <- as.POSIXct(test,format="%Y-%m-%dT%H:%M:%OS")
op <- options(digits.secs = 3)
z
#[1] "2013-12-25 04:32:16.5 EST"

这篇关于将日期时间字符串转换为R中的POSIXct日期/时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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