如何使用动物园从 2 列中读取日期时间? [英] How to read date time from 2 columns with zoo?

查看:30
本文介绍了如何使用动物园从 2 列中读取日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含分钟汇率的 csv 文件

I have a csv file contains minutes exchange rate

<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>
EURUSD,20110103,000000,1.3353,1.3354,1.3353,1.3354,4
EURUSD,20110103,000100,1.3355,1.3356,1.3355,1.3356,4
EURUSD,20110103,000200,1.3355,1.3356,1.3350,1.3350,4
EURUSD,20110103,000300,1.3349,1.3349,1.3348,1.3348,4
EURUSD,20110103,000400,1.3347,1.3348,1.3347,1.3348,4
...

尝试在下面做但对我不起作用.如何从 2 列中读取日期时间?

Trying to do below but not working for me. How to read date time from 2 columns?

rate <- read.zoo("data.csv",sep=",",tz="",header=T, format='%Y%m%d %H%M%S', index = 2:3)

以上代码生成错误:index 在数据行有 5 个错误条目:1 2 3 4 5

推荐答案

您需要指定 colClasses 以保留第三列的前导零,并删除第一列(因为您可以t 在动物园对象中同时具有数字和字符).请注意,如果指定了多个索引列,则默认操作是将它们粘贴在一起,并在它们之间留一个空格.

You need to specify colClasses to keep the leading zeros on the third column, and to remove the first column (since you can't have both numbers and characters in a zoo object). Note that the default action if multiple index columns are specified is to paste them together with a space between them.

Lines <- "<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>
EURUSD,20110103,000000,1.3353,1.3354,1.3353,1.3354,4
EURUSD,20110103,000100,1.3355,1.3356,1.3355,1.3356,4
EURUSD,20110103,000200,1.3355,1.3356,1.3350,1.3350,4
EURUSD,20110103,000300,1.3349,1.3349,1.3348,1.3348,4
EURUSD,20110103,000400,1.3347,1.3348,1.3347,1.3348,4
"

rate <- read.zoo(text=Lines, sep=",", header=TRUE, 
  index.column=1:2, format="%Y%m%d %H%M%S", tz="", 
  colClasses = rep(c("NULL", "character", "numeric"), c(1, 2, 5)))

修改:简化.

这篇关于如何使用动物园从 2 列中读取日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆