我有一个data
看起来像这样的数据框:
dim(data)
# [1] 66955 2
library(chron)
data <- structure(list(`Date-Time` = structure(c(11320.6592476852,
11320.6661921296, 11324.3958564815, 11324.4022569444),
format = structure(c("y/m/d", "h:m:s"), .Names = c("dates", "times")),
origin = structure(c(1, 1, 1970), .Names = c("month", "day", "year")),
class = c("chron", "dates", "times")), Price = c(14.8125, 14.875,
14.9375, 14.9375)), .Names = c("Date-Time", "Price"),
row.names = 18620:18623, class = "data.frame")
第一列属于data
“chron”“dates”“times”类。日期时间格式为 (yy/mm/dd h:m:s)。我正在尝试转换data[,2]
为 xts 对象以便使用该函数makeReturns
。
我试过了:
library(xts)
as.xts(data[,2] , order.by=data[,1], format=c(dates="y/m/d", times="h:m:s"))
但这给了我以下信息(对于上面的行):
[,1]
(NA NA) 14.8125
(NA NA) 14.8750
(01/01/02 09:30:02) 14.9375
(01/01/02 09:39:15) 14.9375
也就是说,它不会将前两行(日期时间格式 (00/12/29 15:49:19) 和 (00/12/29 15:59:19))识别为 yy/mm/dd (和 h:m:s)。实际上在前两行中,年份是“00”(2000),并且没有转换,而在第三和第四行,年份是“01”(2001),他转换了,但是作为天或月。
如何确保所有内容都转换为原始格式不会改变?我尝试了几种变体,format=" "
但没有任何效果。