Reading csv with date and time - r

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.
I am using the code in R
EURUSD <- as.xts(read.zoo("myfile.csv",sep=",",tz="",header=T))
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
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.
I hope someone would help me.

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"
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')
This worked for me and I read a file that has about 900K rows.

It looks like the error is due to R not recognising what format your date column is in (it can't work out -- date/month/year? month/date/year? etc).
You can tell R what format it is in using the format argument to read.zoo (see ?strptime for the specifiers you can use).
For example, if it was date/month/year hour(24-hour clock):minutes, you could do:
EURUSD <- as.xts(read.zoo(file_name,
sep=',',
tz='',
header=T,
format='%d/%m/%Y %H:%M:%S')) # see the 'format' argument?
(Note - in your question the snippet of csv data you showed isn't comma-delimited).

Read the file without using as.xtc, when the date column is like a character. And then convert the dates to POSIXlt class with this function:
library("chron")
DateConvert<-function(x){
dt<-strsplit(x,split = "T")
dt<-unlist(dt)
d1<-dt[1:length(dt) %% 2==1 ]
d2<-dt[1:length(dt) %% 2==0 ]
a<-as.POSIXlt(chron(dates.=d1, times.=d2, format = c(dates = "y-m-d", times = "h:m:s")))
return(a)
}
DateConvert('Your column')
and just then use the function as.xts on you data.

Related

how to load dates in a readable format from excel into r?

I have an excel spreadsheet that has different date formats. R reads in the dates correctly when they're formatted as m/dd/yy, but when they're m/d/yy, it reads them as a code like this: 36251.
To fix it, I found this, which seems useful, but it is only for one date and I have multiple.
How to convert Excel date format to proper date in R
This was what was suggested:
as.Date(42705, origin = "1899-12-30")
So I tried this:
stockindices0$Date <- as.Date(stockindices0$Date , origin = "12-30-99")
but got the error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I also tried it with the year as 1899 with no success.
You can try this
as.Date(stockindices0$Date, format = "%d/%m/%y")

as.Date() not giving desired result. (giving NA)

I read a .xlsx file containing Date columns into R and converted it into dataframe.
Some date columns are being read correctly but most of the others are getting converted to "43116" format.
Any attempt to convert it into Date using as.Date(, origin= <>, format=<>) is returning NA.
I have tried all possible solutions like using 'stringAsFactors = FALSE', POSIT thing and checking the excel file for date formats but nothing worked.
Please help.
It is difficult to recreate the problem if no data is provided, but if you want to convert the number 43129 or the character "43129" to a R date you should do the following:
a <- 43129
b <- '43129'
format(as.Date(a, origin = "1899-12-30"), '%Y-%m-%d')
[1] "2018-01-29"
format(as.Date(as.integer(b), origin = "1899-12-30"), '%Y-%m-%d')
[1] "2018-01-29"
I used the format yyyy-mm-dd, but any other date format could be used if you format it properly.
Hope it helps!

subset data based on y/m/d/h and get error ouputs [duplicate]

I have a dataset called EPL2011_12. I would like to make new a dataset by subsetting the original by date. The dates are in the column named Date The dates are in DD-MM-YY format.
I have tried
EPL2011_12FirstHalf <- subset(EPL2011_12, Date > 13-01-12)
and
EPL2011_12FirstHalf <- subset(EPL2011_12, Date > "13-01-12")
but get this error message each time.
Warning message:
In Ops.factor(Date, 13- 1 - 12) : > not meaningful for factors
I guess that means R is treating like text instead of a number and that why it won't work?
Well, it's clearly not a number since it has dashes in it. The error message and the two comments tell you that it is a factor but the commentators are apparently waiting and letting the message sink in. Dirk is suggesting that you do this:
EPL2011_12$Date2 <- as.Date( as.character(EPL2011_12$Date), "%d-%m-%y")
After that you can do this:
EPL2011_12FirstHalf <- subset(EPL2011_12, Date2 > as.Date("2012-01-13") )
R date functions assume the format is either "YYYY-MM-DD" or "YYYY/MM/DD". You do need to compare like classes: date to date, or character to character. And if you were comparing character-to-character, then it's only going to be successful if the dates are in the YYYYMMDD format (with identical delimiters if any delimiters are used).
The first thing you should do with date variables is confirm that R reads it as a Date. To do this, for the variable (i.e. vector/column) called Date, in the data frame called EPL2011_12, input
class(EPL2011_12$Date)
The output should read [1] "Date". If it doesn't, you should format it as a date by inputting
EPL2011_12$Date <- as.Date(EPL2011_12$Date, "%d-%m-%y")
Note that the hyphens in the date format ("%d-%m-%y") above can also be slashes ("%d/%m/%y"). Confirm that R sees it as a Date. If it doesn't, try a different formatting command
EPL2011_12$Date <- format(EPL2011_12$Date, format="%d/%m/%y")
Once you have it in Date format, you can use the subset command, or you can use brackets
WhateverYouWant <- EPL2011_12[EPL2011_12$Date > as.Date("2014-12-15"),]

read.csv2 date formatting in R

I wish to import my csv file into a data frame but the date in my csv file is in a non-standard format.
The date in the first column is in the following format:
08.09.2016
One of the arguments in my read.csv2 functions is to specify the classes and when I specify this column as a date I receive the following error upon execution:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I'm guessing it doesn't like converting the date from factor class to date class.
I've read a little about POSIXlt but I don't understand the details of the function.
Any ideas how to convert the class from factor to date??
When you convert character to date, you need specify format if it is not standard. The error you got is the result of as.Date("08.09.2016"). But if you do as.Date("08.09.2016", format = "%m.%d.%Y"), it is fine.
I am not sure whether it is possible to pass format to read.csv2 for correct date formatting (maybe not). I would simply read in this date column as factor, then do as.Date(as.character(), format = "%m.%d.%Y") on this column myself.
Generally we use the following format "dd/mm/yy" how can I reorganise the date to that format?
Use format(, format = "%d/%m/%y").
A complete example:
format(as.Date("08.09.2016", format = "%m.%d.%Y"), format = "%d/%m/%y")
# [1] "09/08/16"

Importing date vector from Excel into R

I'm reading an Excel spreadsheet into R, one of the columns contains dates in format
03-Jan-11
07-Feb-11
07-Mar-11
07-Mar-11
04-Apr-11
e.t.c
However when imported into R using read.xlsx2 they become numeric:
40546
40581
40609
40609
e.t.c.
How can I convert the numeric dates into their old format?
I have tried:
as.Date( dates, origin= "2011-01-03")
However this gave error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
and
as.Date(as.numeric(Directories$Insertion.Date),origin="2011/01/03")
Did not preserve the dates
Does anyone know how I can do this in R
Many thanks.
In the end solved this with
as.Date( as.numeric (as.character(Directories$Insertion.Date) ),origin="1899-12-30")
Thanks very much to the commenters for clearing up my confusion!

Resources