转换日期时间格式以在 xts 中使用 [英] Convert date-time format for use in xts

查看:38
本文介绍了转换日期时间格式以在 xts 中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的数据转换为 xts,但我不断收到order.by 需要适当的基于时间的对象"错误,因此我正在尝试将我的日期时间转换为正确的格式.

I am trying to convert my data into an xts but I keep getting a "order.by requires an appropriate time-based object" error so I am trying to convert my date time into the correct format.

我的数据如下所示:

 Date      Time      Value
 20090202  9:30      1
 20090202  9:31      2
 20090202  9:32      3
 20090202  9:33      4
 20090202  9:34      5
 20090202  9:35      6

我也这样做了: data.frame(cbind(theData$Date, theData$Time)) 产生了:

I have done this as well: data.frame(cbind(theData$Date, theData$Time)) which yields:

    1         2          
 20090202    09:30
 20090202    09:31
 20090202    09:32
 20090202    09:33
 20090202    09:34
 20090202    09:35

如何将这些合并到一个列中,以便:

how to I merge these into one columnn so its:

       1
 20090202 09:30
 20090202 09:31
 20090202 09:32
 20090202 09:33
 20090202 09:34
 20090202 09:35

这样我就可以把它放到一个 xts() 中

so that I can then put it into an xts()

推荐答案

你只需要在日期和时间列上使用paste,然后在上面调用as.POSIXct

You just need to use paste on the date and time column, then call as.POSIXct on that.

theData <- read.table(text="Date      Time      Value
 20090202  9:30      1
 20090202  9:31      2
 20090202  9:32      3
 20090202  9:33      4
 20090202  9:34      5
 20090202  9:35      6", header=TRUE, as.is=TRUE)
theData$DateTime <- paste(theData$Date, theData$Time)
xts(theData$Value, as.POSIXct(theData$DateTime, format="%Y%m%d %H:%M"))

这篇关于转换日期时间格式以在 xts 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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