zoo / xts微秒读取问题

时间:2012-06-21 10:38:57

标签: r time-series xts zoo strptime

数据看起来像

           Time  Set1    Set2   
10:19:38.551629 16234   16236   
10:19:41.408010 16234   16236   
10:19:47.264204 16234   16236   

我正在尝试将其加载到动物园中。

orig <- read.zoo("~/sample.txt",sep="",header=TRUE,index.column=1,format="%H:%M:%S.%6f")

Error in read.zoo("~/sample.txt", sep = "", header = TRUE, index.column = 1,  : 
  index has 3 bad entries at data rows: 1 2 3 ...

我已检查过所有相关帖子 1. R issue with rounding milliseconds 2. Milliseconds puzzle when calling strptime in R 3. How to parse milliseconds in R?

然而这没有用。 任何建议

1 个答案:

答案 0 :(得分:4)

您希望索引是时间类,例如POSIXctPOSIXlt。此外,您的format参数不太正确。试试这个

read.zoo("~/sample.txt", header = TRUE, format="%H:%M:%OS", FUN=as.POSIXct)

对于提供的样本数据,提供

read.zoo(text="           Time  Set1    Set2   
10:19:38.551629 16234   16236   
10:19:41.408010 16234   16236   
10:19:47.264204 16234   16236   ", header = TRUE, format="%H:%M:%OS", FUN=as.POSIXct)
#                            Set1  Set2
#2012-06-21 10:19:38.551629 16234 16236
#2012-06-21 10:19:41.408010 16234 16236
#2012-06-21 10:19:47.264204 16234 16236