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()
orstrftime()
:
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.
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.
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.
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"
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.
How to change date format in R from 01-Oct-21 to 02-10-2021
Using base R. You might need to convert your original column into Date format using
old_date <- as.Date('01-Oct-21', format = '%d-%b-%y')
Then you use the format
function to get into what you what
format(old_date, '%d-%m-%Y')
It will look slightly different if your dates are in a data frame.
Related Topics
Manually Setting Group Colors For Ggplot2
Merging Two Data Frames Using Fuzzy/Approximate String Matching in R
Can You Pass-By-Reference in R
Error - Replacement Has [X] Rows, Data Has [Y]
Displaying Text Below the Plot Generated by Ggplot2
Create Group Names For Consecutive Values
Calculating Statistics on Subsets of Data
How to Move Cells With a Value Row-Wise to the Left in a Dataframe
What Does the Dplyr Period Character "." Reference
Ggplot Combining Two Plots from Different Data.Frames
How to Use an Image as a Point in Ggplot
Subscript Letters in Ggplot Axis Label
R on Macos Error: Vector Memory Exhausted (Limit Reached)
Dplyr: "Error in N(): Function Should Not Be Called Directly"
How to Unload a Package Without Restarting R