简体   繁体   English

将制表符分隔为R中的XTS?

[英]Convert tab delimited to XTS within R?

I have tab delimited file in format of: 我有制表符分隔文件,格式为:

Date    Time    Last    LastSize    TotVol  Bid Ask TickID  BidSize AskSize
8/23/2012   0:00:00 0.95711 1   20670   0.95711 0.95742 0   0   0
8/23/2012   0:00:04 0.9571  1   20671   0.9571  0.9574  0   0   0

I am using the function to create an XTS within R. 我正在使用该函数在R中创建XTS。

> EURUSD <- as.xts(read.zoo("C:\\Users\\caustic\\Documents\\DTN\\IQFeed\\EURUSD.FXCM_1.txt",
+                           sep='\t', 
+                           tz='',   
+                           header=T,
+                           format='%d/%m/%Y %H:%M:%S'))

I get an error of: Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format 我收到以下错误:as.POSIXlt.character(x,tz,...)中的错误:字符串不是标准的明确格式

The question is: How do I combine the data and time into needed POSIX format? 问题是:如何将数据和时间组合成所需的POSIX格式? Thanks 谢谢

Since OP hasn't really updated his answer, I will add one way of doing this. 由于OP尚未真正更新他的答案,因此我将添加一种执行此操作的方法。 To convert tab delimited (or CSV ) content to xts , I prefer to first read data as normal data frame and then use .xts function to convert the data.frame to xts 要将制表符分隔的内容(或CSV)转换为xts ,我更喜欢先将数据读取为普通数据帧,然后使用.xts函数将data.frame转换为xts

.xts is currently undocumented function though. .xts目前是未.xts功能。

require(xts)
filecontent <- 'Date    Time    Last    LastSize    TotVol  Bid Ask TickID  BidSize AskSize
8/23/2012   0:00:00 0.95711 1   20670   0.95711 0.95742 0   0   0
8/23/2012   0:00:04 0.9571  1   20671   0.9571  0.9574  0   0   0'

DF <- read.table(text = filecontent, header = TRUE, stringsAsFactors = FALSE)

DFINDEX <- paste(DF$Date, DF$Time, sep = " ")

DF.XTS <- .xts(x = DF[, 3:10], index = as.POSIXct(DFINDEX, format = "%m/%d/%Y %H:%M:%S", tzone = "GMT"))

DF.XTS
##                        Last LastSize TotVol     Bid     Ask TickID BidSize AskSize
## 2012-08-23 08:00:00 0.95711        1  20670 0.95711 0.95742      0       0       0
## 2012-08-23 08:00:04 0.95710        1  20671 0.95710 0.95740      0       0       0

正是示例4在cran.r-project.org/web/packages/zoo/vignettes/zoo-read.pdf中有效,感谢Joshua Ulrich的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM