zoo/xts microsecond read issue

白昼怎懂夜的黑 提交于 2019-12-10 18:52:12

问题


The data looks like

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

I am trying to load this into zoo.

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 ...

I have checked all the relevant posts 1. R issue with rounding milliseconds 2. Milliseconds puzzle when calling strptime in R 3. How to parse milliseconds in R?

However this does not help. Any Suggestions


回答1:


You want the index to be a time class such as POSIXct or POSIXlt. Also, your format argument wasn't quite right. Try this

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

Which, for the sample data provided, gives

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


来源:https://stackoverflow.com/questions/11136340/zoo-xts-microsecond-read-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!