I have multiple csv
files which I need to read into R. The first column of the files contain dates and times, which I am converting into POSIXlt
when I have loaded the data frame. Each of my csv
files have the dates and times formatted in the same way in Excel, however, some files are read in differently.
For example,
My file looks like this once imported:
date value
1 2011/01/01 00:00:00 39
2 2011/01/01 00:15:00 35
3 2011/01/01 00:30:00 38
4 2011/01/01 00:45:00 39
5 2011/01/01 01:00:00 38
6 2011/01/01 01:15:00 38
Therefore, the code I use to amend the format is:
DATA$date <- as.POSIXlt(DATA$date,format="%Y/%m/%d %H:%M:%S")
However, some files are being read in as:
date value
1 01/01/2011 00:00 39
2 01/01/2011 00:15 35
3 01/01/2011 00:30 38
4 01/01/2011 00:45 39
5 01/01/2011 01:00 38
6 01/01/2011 01:15 38
Which means my format section of my code does not work and gives an error. Therefore, is there anyway to automatically detect which format the date
column is in? Or, is there a way of knowing how it will be read, since the format of the column in Excel is the same on both.