R 计时到xts-保留日期时间格式

R 计时到xts-保留日期时间格式,r,xts,R,Xts,我有一个数据框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

我有一个数据框
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")
但这给了我以下信息(对于上面的行):

也就是说,它不将前两行(日期时间格式为(00/12/29 15:49:19)和(00/12/29 15:59:19))识别为yy/mm/dd(和h:m:s)。事实上,在前两行中,年份是“00”(2000年),并且没有转换,而在第三行和第四行中,年份是“01”(2001年),他转换为一天或一个月


我如何确保所有内容都转换为其原始格式,从而使原始格式不发生更改?我在
format=“”
中尝试了几种不同的方法,但都没有效果。

chron似乎没有
as.POSIXct
方法,默认的
as.POSIXct
方法可能无法按您希望的方式处理时区,因此,您需要将
数据的第一列
转换为字符,然后强制转换为
POSIXct

> xts(data[,2], as.POSIXct(as.character(data[,1]), format="(%y/%m/%d %H:%M:%S)"))
                       [,1]
2000-12-29 15:49:19 14.8125
2000-12-29 15:59:19 14.8750
2001-01-02 09:30:02 14.9375
2001-01-02 09:39:15 14.9375

非常感谢,这非常有帮助!
                       [,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
> xts(data[,2], as.POSIXct(as.character(data[,1]), format="(%y/%m/%d %H:%M:%S)"))
                       [,1]
2000-12-29 15:49:19 14.8125
2000-12-29 15:59:19 14.8750
2001-01-02 09:30:02 14.9375
2001-01-02 09:39:15 14.9375