我正在尝试将我的数据转换为 xts,但我不断收到“order.by 需要一个适当的基于时间的对象”错误,因此我正在尝试将我的日期时间转换为正确的格式。
我的数据是这样的:
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)) 结果是:
1 2
20090202 09:30
20090202 09:31
20090202 09:32
20090202 09:33
20090202 09:34
20090202 09:35
我如何将这些合并到一个列中,以便它:
1
20090202 09:30
20090202 09:31
20090202 09:32
20090202 09:33
20090202 09:34
20090202 09:35
这样我就可以将它放入 xts()
最佳答案
您只需在日期和时间列上使用paste
,然后对其调用as.POSIXct
。
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"))
关于r - 转换日期时间格式以在 xts 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18261160/