读取带有日期和时间的 csv [英] Reading csv with date and time

查看:52
本文介绍了读取带有日期和时间的 csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中工作并阅读第一列中有日期和时间的 csv.我想先在 R 中导入这个 csv 文件,然后将其转换为 zoo obect.

I am working in R and reading csv which has date and time in its first column. I want to import this csv file in R first and then convert it to zoo obect.

我正在使用 R 中的代码

I am using the code in R

EURUSD <- as.xts(read.zoo("myfile.csv",sep=",",tz="",header=T))

我的 csv 文件包含以下格式的数据:

My csv file contain data in the format:

Date,Open,Low,High,Close
2006-01-02 10:01:00,2822.9,2825.45,2822.1,2824.9
2006-01-02 10:02:00,2825,2825.9,2824,2824.95
2006-01-02 10:03:00,2824.55,2826.45,2824,2826.45
2006-01-02 10:04:00,2826.45,2826.45,2824.9,2825.5
2006-01-02 10:05:00,2825.15,2825.5,2824,2824.85
2006-01-02 10:06:00,2824.7,2825.5,2823.7,2823.8
2006-01-02 10:07:00,2823.95,2824.45,2823.55,2824
2006-01-02 10:08:00,2824,2824.85,2823.5,2824.85
2006-01-02 10:09:00,2824.25,2825.45,2824,2825.45
2006-01-02 10:10:00,2825.2,2827,2825,2827

当我运行上述命令将数据导入 R 时,出现以下错误:

When I run the above command to import the data in to R I get the folowwwing error :

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

我试图找到解决问题的所有方法.我在网上阅读了很多博客,但没有一种方法适合我.

I tried to find all the ways to sort out the issue. I read so many blogs over net but none of the method works for me.

我希望有人能帮助我.

推荐答案

虽然这似乎是一个旧帖子,但我想分享我的经验,因为我经历了一个类似的非常令人沮丧的过程,试图将时间序列 csv 数据加载到R.上面的问题是excel把日期和时间的格式改成了下面的%m/%d/%Y %H:%M,基本上是秒掉了.如果您使用这种格式读取文件并且您有第二个分辨率数据,您将获得多个相似的日期时间组合.所以你不能简单地使用忽略秒的格式,因为它会给出以下错误消息.字符串不是标准的明确格式"

Although this seems to be an old post, but I want to share my experience since I went through a similar very frustrating process trying to load time series csv data into R. The problem above is that excel changes the format of the date and time to the following %m/%d/%Y %H:%M, basically it drops the seconds. If you read a file with this format and you have a second resolution data you get multiple date time combinations that are similar. so you cannot simply use the format that ignores seconds because it gives the following error message . "character string is not in a standard unambiguous format"

解决办法是回到excel,将日期时间列的格式改为%m/%d/%Y %H:%M:%S.您可以通过选择与所需格式最接近的日期时间默认格式(在本例中为 %m/%d/%Y %H:%M,然后手动添加 :ss 结尾.将文件另存为 csv 文件,然后使用以下命令读取它:

The solution is to go back to excel and change the format of the date time column to be %m/%d/%Y %H:%M:%S. You can do that by choosing the closest date time default formats to the desired format (in this case it is %m/%d/%Y %H:%M and then manually add :ss at the end. Save the file as a csv file and then read it using the following command:

Data<-read.zoo("file.csv", tz="", header=TRUE,format='%m/%d/%Y %H:%M:%S')

这对我有用,我读取了一个大约有 90 万行的文件.

This worked for me and I read a file that has about 900K rows.

这篇关于读取带有日期和时间的 csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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