Convert tab delimited to XTS within R?

二次信任 提交于 2019-12-11 05:19:56

问题


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.

> 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

The question is: How do I combine the data and time into needed POSIX format? Thanks


回答1:


Since OP hasn't really updated his answer, I will add one way of doing this. 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

.xts is currently undocumented function though.

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



回答2:


It was example 4 that works in cran.r-project.org/web/packages/zoo/vignettes/zoo-read.pdf Thanks to Joshua Ulrich for help



来源:https://stackoverflow.com/questions/12115716/convert-tab-delimited-to-xts-within-r

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