有没有人知道如何将其转换为xts R对象?
#format: symbol,interval,date,time,open,high,low,close,,
# cat test.csv
AD.F,5,20141008,093500,0.8736,0.8738,0.8732,0.8733,0,0
AD.F,5,20141008,094000,0.8732,0.8736,0.8731,0.8735,0,0
AD.F,5,20141008,094500,0.8736,0.8737,0.8733,0.8737,0,0
AD.F,5,20141008,095000,0.8736,0.8747,0.8736,0.8746,0,0
AD.F,5,20141008,095500,0.8746,0.8748,0.8742,0.8743,0,0
AD.F,5,20141008,100000,0.8744,0.875,0.8742,0.8747,0,0
AD.F,5,20141008,100500,0.8747,0.8747,0.874,0.8745,0,0
AD.F,5,20141008,101000,0.8744,0.8744,0.874,0.8743,0,0
AD.F,5,20141008,101500,0.8744,0.875,0.8744,0.875,0,0
AD.F,5,20141008,102000,0.8751,0.8752,0.8746,0.8751,0,0
我尝试了read.csv()
,但我遇到时间格式问题(read.csv
将时间转换为数字,例如从" 091500"到91500)。
答案 0 :(得分:2)
我们可以指定使用colClasses
将日期和时间列视为字符。这也处理索引。如果第1列中有多个值,则会将每个值拆分为单独的列:
library(zoo)
z <- read.zoo("input.csv", sep = ",", split = 1, index = 3:4, tz = "",
format = "%Y%m%d %H%M%S", colClasses = rep( c(NA, "character", NA), c(2, 2, 6)))
可以通过as.xts(z)
将其转换为xts。有关详细信息,请参阅read.zoo
上的帮助文件和动物园插图Reading Data in zoo
。
或者,使用与colClasses
相同的read.table
。