将制表符分隔为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
我正在使用该功能在R中创建XTS.
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'))
我收到以下错误: as.POSIXlt.character(x,tz,...)中的错误: 字符串不是标准的明确格式
I get an error of: Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format
问题是:如何将数据和时间合并为所需的POSIX格式? 谢谢
The question is: How do I combine the data and time into needed POSIX format? Thanks
推荐答案
由于OP尚未真正更新他的答案,因此我将添加一种执行此操作的方法.
要将制表符分隔的(或CSV)内容转换为xts
,我更喜欢先将数据读取为普通数据帧,然后使用.xts
函数将data.frame
转换为xts
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
目前尚未公开功能.
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
这篇关于将制表符分隔为R中的XTS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!