Changing date formats in R
Dates come in all sorts of formats, but R generally prefers dates in the format YYYY-MM-DD. Here’s a quick method to convert date formats in R.
Let’s say we have a CSV file with dates in the format %d-%m-%Y
(eg: 21/06/2021) and would like to convert these Australian-style dates into something more widely recognised internationally (ie: %Y-%m-%d
). This isn’t as tricky as it seems.
Let’s begin by importing our data:
library(data.table)
df <- fread("data.csv")
If we use the head(df)
function, we can see the structure of the data (which is very simple for this example):
date MB
1: 01/03/2021 9128.039
2: 02/03/2021 124144.419
3: 03/03/2021 135989.970
4: 04/03/2021 2925.900
5: 05/03/2021 3374.590
6: 06/03/2021 2186.940
The dates are all in %d-%m-%Y
format. To change this, we firstly need to parse the data:
date <- strptime(as.character(df$date), "%d/%m/%Y")
Next, the dates are reformatted:
date <- format(date, "%Y-%m-%d")
The final step is to put the newly reformatted data into a new data frame. Since there is only one other column in this data frame, I’ll rename it as follows:
MB <- df$MB
Now I can put my columns into a new data frame:
df2 <- data.frame(date, MB)
Let’s check that with head(df2)
:
date MB
1 2021-03-01 9128.039
2 2021-03-02 124144.419
3 2021-03-03 135989.970
4 2021-03-04 2925.900
5 2021-03-05 3374.590
6 2021-03-06 2186.940
Voila! The date format in our dataset has been reformatted. Unfortunately R only recognises dates in the following two formats:
- %Y-%m-%d
- %Y/%m/%d
Anything else may lead to one of those annoying “character string is not in a standard unambiguous format” errrors whenever you start analysing your data. This why it’s important to correctly format your dates as soon as you open a data file in R.
Comments
No comments have yet been submitted. Be the first!