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.

 

 

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.


8 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
  • Estela Covre Foltran
    July 6, 2021 9:29 am

    Hi Joachim,
    Thank you for your easy-to-follow tutorials.
    I have a question regarding the as.Date transformation.

    I am trying to convert character to date class, apparently works. However, the formate and the date itself are wrong.
    My data set is from 2014 to 2016 and is formated day. month and year. Afterward, it is converted to a completely different date starting in 2020 and converted to year, day, month.

    [993] “07/03/2016” “07/03/2016” “07/03/2016” “07/03/2016” “04/04/2016” “04/04/2016” “04/04/2016” “04/04/2016”
    [ reached getOption(“max.print”) — omitted 860 entries ]
    > class(DB_Succion_Cups$DATE_MED)
    [1] “character”
    > Dates_new= as.Date(DB_Succion_Cups$DATE_MED, format = “%d/%m/%y”)
    > class(Dates_new)
    [1] “Date”
    > Dates_new
    [1] “2020-02-10” “2020-02-10” “2020-02-10” “2020-02-10” “2020-02-10” “2020-02-10” “2020-02-10” “2020-02-10”
    [9] “2020-02-10” “2020-02-10”

    I really appreciate your help. Thanks 🙂
    Estela

    Reply
    • Hi Estela,

      Thanks a lot for the very nice feedback, glad you find my tutorials helpful! 🙂

      I think you have to use an uppercase Y instead of a lowercase y when specifying the format of the date.

      The following code works for me:

      x <- c("07/03/2016", "07/03/2016", "07/03/2016", "07/03/2016", "04/04/2016", "04/04/2016", "04/04/2016", "04/04/2016")
      as.Date(x, format = "%d/%m/%Y")

      I hope that helps!

      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.

Top