Change Format of Dates in R (4 Examples)

 

In this post you’ll learn how to modify the structure of date objects in R programming.

The page will contain the following content:

Let’s get started…

 

Example Data

First, I’ll have to create some example data:

my_date <- as.Date("2020-05-27")        # Create example date
my_date                                 # Print example date
# "2020-05-27"

As you can see based on the previous output of the RStudio console, our example data is a single date object (i.e. the 27th of May 2020).

Note that we have used the as.Date function to create our example date. This is necessary to create a data object with the Date class (instead of a character string).

We can check the data type of our date using the class function:

class(my_date)                          # Check class of date
"Date"

Our example data has the Date class – looks good. Let’s move on to the examples on how to format date objects in R…

 

Example 1: Reorder Day, Month & Year

In this example, I’ll explain how to exchange the ordering of our year/month/day structure. In this example, I’ll show the day at the first position, the month at the second position, and the year at the third position.

To achieve this, we can use the format function as shown below:

format(my_date, "%d-%m-%Y")             # Applying format function
# "27-05-2020"

Have a look at the previously shown output of the format function: It shows our date object with reversed order of the year, month, and day.

 

Example 2: Display Month as Word Instead of Number

The following R syntax shows how to show the name of the month of our date instead of a number:

format(my_date, "%Y-%h-%d")             # Applying format function
# "2020-May-27"

As you can see, 05 was replaced by May.

 

Example 3: Add Hours, Minutes & Seconds to Date

In this example, I’ll explain how to append hours, minutes, and seconds to Date objects:

format(my_date, "%Y-%m-%d-%H-%M-%S")    # Applying format function
# "2020-05-27-00-00-00"

In this example, the values for hours, minutes, and seconds are 00, because our input date did not specify these time values.

 

Example 4: Change Date Separators

In Example 4, I’ll illustrate how to replace date separators:

format(my_date, "%Y/%m/%d")             # Applying format function
# "2020/05/27"

The previous R syntax replaced “-” by “/”.

 

Video & Further Resources

In case you need more explanations on the R codes of this tutorial, you could watch the following video of my YouTube channel. In the video, I illustrate the R code of this tutorial.

 

The YouTube video will be added soon.

 

Furthermore, you might want to have a look at the related tutorials that I have published on this homepage. You can find a selection of articles below:

 

This article illustrated how to adjust date objects in the R programming language.

I explained how to do that based on a single date object. However, please note that it would also be possible to apply the same type of R code to date vectors or variables and columns of a data frame.

Let me know in the comments below, in case you have further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top