使用Zoo读取R中的CSV [英] Reading CSV in R with zoo

查看:204
本文介绍了使用Zoo读取R中的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的CSV:

I have a CSV in the following format:

TICKER,PER,DATE,TIME,CLOSE
SYMBOL,1,20160104,1002,14180.0000000
SYMBOL,1,20160104,1003,14241.0000000

我想读成一个时间序列:

I would like to read it into a time series:

f <- function(a, b) {
    c <- paste(a, b)
    return(strptime(c, format = "%Y%m%d %H%M"))
}
d <- read.zoo("test.csv", FUN = f, index.column = list("DATE", "TIME"))

我得到的是index does not match data.为什么?

推荐答案

您需要指定header = TRUEsep = ",",因为它们不是read.zoo的默认值,就像read.csv的默认值一样.

You need to specify header = TRUE and sep = ",", since they are not the defaults for read.zoo like they are for read.csv.

d <- read.zoo(text="TICKER,PER,DATE,TIME,CLOSE
SYMBOL,1,20160104,1002,14180.0000000
SYMBOL,1,20160104,1003,14241.0000000",
  FUN = f, index.column = list("DATE", "TIME"),
  header=TRUE, sep=",")
d
#                     TICKER PER CLOSE
# 2016-01-04 10:02:00 SYMBOL 1   14180
# 2016-01-04 10:03:00 SYMBOL 1   14241

这篇关于使用Zoo读取R中的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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