as.Date Function in R (Example)

 

In this tutorial, I’ll illustrate how to convert a character to the Date class using the as.Date function in R.

The tutorial will contain the following content:

Let’s take a look at some R codes in action.

Definition & Basic R Syntax of as.Date Function

 

Definition: The as.Date R function converts character strings to the Date class.

 

Basic R Syntax: Please find the basic R programming syntax of the as.Date function below.

as.Date("2020-12-10")               # Basic R syntax of as.Date function

 

In the remaining post, I’ll show an example for the application of the as.Date function in the R programming language.

 

Example Data

The following data will be used as basement for this R programming tutorial:

my_date <- "2020-10-05"             # Create example date
my_date                             # Print example date
# "2020-10-05"

The previous RStudio console output shows the structure of our example data: It’s a data object containing a single character string with year-month-day format.

Let’s check the data type of our example data:

class(my_date)                      # Check class of example date
# "character"

It’s a character string…

 

Example: Convert Character String to Date Using as.Date() Function

The following R syntax illustrates how to convert a character string to a Date object in R. For this task, we can use the as.Date function as shown below:

my_date_new1 <- as.Date(my_date)    # Convert character string
my_date_new1                        # Print new date
# "2020-10-05"

The previous output of the RStudio console looks exactly as the output of our original input data. However, by checking the data class we see the difference:

class(my_date_new1)                 # Check class
# "Date"

The updated data object has the class Date. Looks good!

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. In the video, I’m explaining the contents of the present tutorial.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you may want to read some of the related articles that I have published on this homepage.

 

At this point you should have learned how to change characters to dates in R programming. Let me know in the comments section below, in case you have additional questions. Besides that, please subscribe to my email newsletter to receive updates on the newest tutorials.

 

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.


4 Comments. Leave new

  • Hi Joachim,

    Thank you for your easy to follow tutorials.

    I have a question regarding the as.Date transformation.

    I have imported some data into R, but on import, my dates where converted from Jan-85 , Feb-85, etc… to 31078, 31106, etc… After a gather manipulation they are now shown as 31078, 31106 but the class is a character.

    I have tried the as.Date manipulation to convert 31078 back to Jan-85 but I either get errors or 31078 is converted into today’s date.

    I would therefore really appreciate your help.

    Thank you in advance.

    Best Regards,
    Efi

    Reply
    • Hey Efi,

      Thank you for your comment and the kind words!

      I have tried to reproduce your problem, but it’s difficult to do this without seeing the real data.

      Could you send me a small subset of your data via email to joachim@statisticsglobe.com?

      Regards, Joachim

      Reply
      • Thank you Joachim for this.
        My data was digitally obtained and therefore saved with slashes i.e d/m/y in csv
        I have imported this same data into R and I’d like it to be read as dates not characters.
        This is a vector (2 entire columns) not an object in a given dataset.

        Thereafter I’d like to subtract the dates from the 2 columns from each other, row by row.
        I’ll appreciate your assistance.

        Reply
        • Hi Derrick,

          Could you provide an example how your data looks like? I’m not sure if I got what you mean with the last part of your question.

          Regards

          Joachim

          Reply

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