Converting Date Formats in R

Changing date format in R

There are two steps here:

  • Parse the data. Your example is not fully reproducible, is the data in a file, or the variable in a text or factor variable? Let us assume the latter, then if you data.frame is called X, you can do
 X$newdate <- strptime(as.character(X$date), "%d/%m/%Y")

Now the newdate column should be of type Date.

  • Format the data. That is a matter of calling format() or strftime():
 format(X$newdate, "%Y-%m-%d")

A more complete example:

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

The difference between columns three and four is the type: newdate is of class Date whereas txtdate is character.

Converting Date formats in R

To obtain your required format i.e., 2016-month-day , you can use format function once you have converted vector of strings to Date type.

I hope below code snippet clears your doubt.

> d = c("2016-02-08","2016-02-18","2015-02-08","2016-02-02")
> class(d)
[1] "character"
> d = as.Date(d)
> class(d)
[1] "Date"
> d = format(d,"%Y-%b-%d")
> d
[1] "2016-Feb-08" "2016-Feb-18" "2015-Feb-08" "2016-Feb-02"

Format function converts the date type objects into the required format. Refer to this link for more information on date type formatting.

Convert string into date format in R

The as.Date() function will convert a string into date format, and the format of the output will always be in yyyy-mm-dd format in R (ISO 8601). The format argument in the as.Date() function is to specify the date format of the string input. I remember I initially thought it was specifying the output format, but it's the input format (you can change the output format with a subsequent format() function, however this will convert it back to a string).

Your string looks to be in ddmmyyyy (%d%m%Y) format, this should be what you specify as the format argument in as.Date(). Your format does not include hyphens, so the format argument should also not include hyphens. Note that ddmmyyyy, dd-mm-yyyy, dd/mm/yyyy, dd.mm.yyyy are all different date formats, even though the day, month and year are in the same order, they would be converted to date with formats %d%m%Y, %d-%m-%Y, %d/%m/%Y, and %d.%m.%Y, respectively.

Further advice on working with dates and times is available in the relevant chapters of R for Data Science by Wickham & Grolemund and The R Cookbook by Teetor & Long.

Change of Date format

It should be %I to represent hours as decimal number (01–12), not %H, and %y to
years without century (00–99).

x <- "1/1/2021 12:00:00 AM"

format(strptime(x, "%m/%d/%Y %I:%M:%S %p"), "%m/%d/%y")
[1] "01/01/21"

Note that after you re-foramt the time object, it'll be a pure character string and lose all attributes of a time object.

Convert date format in R from dd.mm.yyyy to yyyy

Also year from the lubridate package works:

library(lubridate)
x <- ymd("2018-08-30")

year(x)
# [1] 2018

How to convert character to date with two different types of date formats in R?

You can do something like:

format_ymd  <- as.Date(dates, format = "%Y-%m-%d")
format_dmy <- as.Date(dates, format = "%d/%m/%Y")
as.Date(ifelse(is.na(format_ymd), format_dmy, format_ymd), origin = "1970-01-01")
# [1] "2022-04-08" "2021-01-26" "2021-07-14" "2021-12-27"

Change Date format - Convert to Date Class

A Date class is always shown like "2020-08-27" in R. That's R's standard Date. To reformat it into something different you can use strftime. It assumes a Date class and outputs a character object with your desired format, e.g.

df1$date2
[1] "2020-08-27" "2020-08-28" "2020-08-29"

class(df1$date2)
[1] "Date"

strftime(df1$date2, format="%m/%d/%Y")
[1] "08/27/2020" "08/28/2020" "08/29/2020"

class(strftime(df1$date2, format="%m/%d/%Y"))
[1] "character"


Related Topics



Leave a reply



Submit