如何将4列表加载到xts对象中?

时间:2014-03-01 00:52:34

标签: r xts

我想开始学习如何使用xts包,但是我很难加载我的数据。

文件大约是6MB,看起来像这样:

Date,Time,Price,Size
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:13,49.6,200
02/18/2014,05:06:14,49.6,200
02/18/2014,05:06:14,49.6,193
02/18/2014,05:44:41,49.62,100
02/18/2014,06:26:36,49.52,100
02/18/2014,06:26:36,49.52,500
02/18/2014,07:09:29,49.6,100
02/18/2014,07:56:40,49.56,300
02/18/2014,07:56:40,49.55,400
02/18/2014,07:56:41,49.54,200
02/18/2014,07:56:43,49.55,100
02/18/2014,07:56:43,49.55,100
02/18/2014,07:56:50,49.55,100
02/18/2014,07:57:12,49.53,100
02/18/2014,07:57:12,49.51,2200
02/18/2014,07:57:12,49.51,100
02/18/2014,07:57:12,49.5,200

这是我尝试的代码行和我得到的错误:

data1<-as.xts(read.zoo("EKSO_0.txt",header = T,sep=",",format="%m/%d/%Y"))

我得到的错误:

Warning message:
In zoo(rval3, ix) :
some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not   unique

如何加载“时间”列?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西。它来自zoo vignette中的示例4。您仍然会收到警告,因为您仍然有重复的日期,时间组合

library(zoo)
library(chron)

f <- function(d, t) as.chron(paste(as.Date(chron(d)), t))
read.zoo("EKSO_0.txt", header = TRUE, sep = ",", index.column = 1:2, FUN = f)

##                     Price Size
## (02/18/14 05:06:13) 49.60  200
## (02/18/14 05:06:13) 49.60  200
## (02/18/14 05:06:13) 49.60  200
## (02/18/14 05:06:14) 49.60  200
## (02/18/14 05:06:14) 49.60  193
## (02/18/14 05:44:41) 49.62  100
## (02/18/14 06:26:36) 49.52  100
## (02/18/14 06:26:36) 49.52  500
## (02/18/14 07:09:29) 49.60  100
## (02/18/14 07:56:40) 49.56  300
## (02/18/14 07:56:40) 49.55  400
## (02/18/14 07:56:41) 49.54  200
## (02/18/14 07:56:43) 49.55  100
## (02/18/14 07:56:43) 49.55  100
## (02/18/14 07:56:50) 49.55  100
## (02/18/14 07:57:12) 49.53  100
## (02/18/14 07:57:12) 49.51 2200
## (02/18/14 07:57:12) 49.51  100
## (02/18/14 07:57:12) 49.50  200
## Warning message:
## In zoo(rval3, ix) :
##   some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique