[r] Changing date format in R

I have some very simple data in R that needs to have its date format changed:

 date midpoint
1   31/08/2011   0.8378
2   31/07/2011   0.8457
3   30/06/2011   0.8147
4   31/05/2011   0.7970
5   30/04/2011   0.7877
6   31/03/2011   0.7411
7   28/02/2011   0.7624
8   31/01/2011   0.7665
9   31/12/2010   0.7500
10  30/11/2010   0.7734
11  31/10/2010   0.7511
12  30/09/2010   0.7263
13  31/08/2010   0.7158
14  31/07/2010   0.7110
15  30/06/2010   0.6921
16  31/05/2010   0.7005
17  30/04/2010   0.7113
18  31/03/2010   0.7027
19  28/02/2010   0.6973
20  31/01/2010   0.7260
21  31/12/2009   0.7154
22  30/11/2009   0.7287
23  31/10/2009   0.7375

Rather than %d/%m/%Y, I would like it in the standard R format of %Y-%m-%d

How can I make this change? I have tried:

nzd$date <- format(as.Date(nzd$date), "%Y/%m/%d")

But that just cut off the year and added zeros to the day:

 [1] "0031/08/20" "0031/07/20" "0030/06/20" "0031/05/20" "0030/04/20"
 [6] "0031/03/20" "0028/02/20" "0031/01/20" "0031/12/20" "0030/11/20"
 [11] "0031/10/20" "0030/09/20" "0031/08/20" "0031/07/20" "0030/06/20"
 [16] "0031/05/20" "0030/04/20" "0031/03/20" "0028/02/20" "0031/01/20"
 [21] "0031/12/20" "0030/11/20" "0031/10/20" "0030/09/20" "0031/08/20"
 [26] "0031/07/20" "0030/06/20" "0031/05/20" "0030/04/20" "0031/03/20"
 [31] "0028/02/20" "0031/01/20" "0031/12/20" "0030/11/20" "0031/10/20"
 [36] "0030/09/20" "0031/08/20" "0031/07/20" "0030/06/20" "0031/05/20"

Thanks!

This question is related to r date format strptime r-faq

The answer is


You could also use the parse_date_time function from the lubridate package:

library(lubridate)
day<-"31/08/2011"
as.Date(parse_date_time(day,"dmy"))
[1] "2011-08-31"

parse_date_time returns a POSIXct object, so we use as.Date to get a date object. The first argument of parse_date_time specifies a date vector, the second argument specifies the order in which your format occurs. The orders argument makes parse_date_time very flexible.


nzd$date <- format(as.Date(nzd$date), "%Y/%m/%d")

In the above piece of code, there are two mistakes. First of all, when you are reading nzd$date inside as.Date you are not mentioning in what format you are feeding it the date. So, it tries it's default set format to read it. If you see the help doc, ?as.Date you will see

format
A character string. If not specified, it will try "%Y-%m-%d" then "%Y/%m/%d" on the first non-NA element, and give an error if neither works. Otherwise, the processing is via strptime

The second mistake is: even though you would like to read it in %Y-%m-%d format, inside format you wrote "%Y/%m/%d".

Now, the correct way of doing it is:

> nzd <- data.frame(date=c("31/08/2011", "31/07/2011", "30/06/2011"), 
+                                       mid=c(0.8378,0.8457,0.8147))
> nzd
        date    mid
1 31/08/2011 0.8378
2 31/07/2011 0.8457
3 30/06/2011 0.8147
> nzd$date <- format(as.Date(nzd$date, format = "%d/%m/%Y"), "%Y-%m-%d")
> head(nzd)
        date    mid
1 2011-08-31 0.8378
2 2011-07-31 0.8457
3 2011-06-30 0.8147

This is really easy using package lubridate. All you have to do is tell R what format your date is already in. It then converts it into the standard format

nzd$date <- dmy(nzd$date)

that's it.


After reading your data in via a textConnection, the following seems to work:

dat <- read.table(textConnection(txt), header = TRUE)
dat$date <- strptime(dat$date, format= "%d/%m/%Y")
format(dat$date, format="%Y-%m-%d")

> format(dat$date, format="%Y-%m-%d")
 [1] "2011-08-31" "2011-07-31" "2011-06-30" "2011-05-31" "2011-04-30" "2011-03-31"
 [7] "2011-02-28" "2011-01-31" "2010-12-31" "2010-11-30" "2010-10-31" "2010-09-30"
[13] "2010-08-31" "2010-07-31" "2010-06-30" "2010-05-31" "2010-04-30" "2010-03-31"
[19] "2010-02-28" "2010-01-31" "2009-12-31" "2009-11-30" "2009-10-31"

> str(dat)
'data.frame':   23 obs. of  2 variables:
 $ date    : POSIXlt, format: "2011-08-31" "2011-07-31" "2011-06-30" ...
 $ midpoint: num  0.838 0.846 0.815 0.797 0.788 ...

Using one line to convert the dates to preferred format:

nzd$date <- format(as.Date(nzd$date, format="%d/%m/%Y"),"%Y/%m/%d")

I believe that

nzd$date <- as.Date(nzd$date, format = "%d/%m/%Y")

is sufficient.


Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to date

How do I format {{$timestamp}} as MM/DD/YYYY in Postman? iOS Swift - Get the Current Local Time and Date Timestamp Typescript Date Type? how to convert current date to YYYY-MM-DD format with angular 2 SQL Server date format yyyymmdd Date to milliseconds and back to date in Swift Check if date is a valid one change the date format in laravel view page Moment js get first and last day of current month How can I convert a date into an integer?

Examples related to format

Brackets.io: Is there a way to auto indent / format <html> Oracle SQL - DATE greater than statement What does this format means T00:00:00.000Z? How to format date in angularjs How do I change data-type of pandas data frame to string with a defined format? How to pad a string to a fixed length with spaces in Python? How to format current time using a yyyyMMddHHmmss format? java.util.Date format SSSSSS: if not microseconds what are the last 3 digits? Formatting a double to two decimal places How enable auto-format code for Intellij IDEA?

Examples related to strptime

Display Python datetime without time What are the "standard unambiguous date" formats for string-to-date conversion in R? Changing date format in R How can I account for period (AM/PM) using strftime?

Examples related to r-faq

What does "The following object is masked from 'package:xxx'" mean? What does "Error: object '<myvariable>' not found" mean? How do I deal with special characters like \^$.?*|+()[{ in my regex? What does %>% function mean in R? How to plot a function curve in R Use dynamic variable names in `dplyr` Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code How should I deal with "package 'xxx' is not available (for R version x.y.z)" warning? How to select the row with the maximum value in each group R data formats: RData, Rda, Rds etc