How to Use as.Date() Function in R (With Examples)


You can use the as.Date() function in R to quickly convert character objects to date objects.

This function uses the following basic syntax:

as.Date(x, format, tryFormats = c("%Y-%m-%d", "%Y/%m/%d")

where:

  • x: The name of the object to be converted to date.
  • format: The format of the date string. If not specified, it will try one of the tryFormats.
  • tryFormats: Formats to try. 

The following examples show how to use this function in different scenarios.

Example 1: Use as.Date() with Recognizable Date Formats

By default, the as.Date() function can easily convert character objects to date objects if the character objects are formatted in one of the following ways:

  • %Y-%m-%d
  • %Y/%m/%d

The following code shows how to use the as.Date() function to convert a character object with a %Y-%m-%d format to a date object:

#define character object in %Y-%m-%d format
x <- "2022-10-15"

#view class of x
class(x)

[1] "character"

#convert character object to date object
my_date <- as.Date(x)

#view new date object
my_date

[1] "2022-10-15"

#view class of my_date
class(my_date)

[1] "Date"

We can see that the character object has been converted to a date object.

The following code shows how to use the as.Date() function to convert a character object with a %Y/%m/%d format to a date object:

#define character object in %Y/%m/%d format
x <- "2022/10/15"

#convert character object to date object
my_date <- as.Date(x)

#view class of my_date
class(my_date)

[1] "Date"

We can see that the character object has been converted to a date object.

For both of these examples, we didn’t need to use the format argument in the as.Date() function because both date formats were recognized by R.

Example 2: Use as.Date() with Unrecognizable Date Formats

When character objects have an unrecognizable date format, you must use the format argument to specify the format.

For example, the following code shows how to use the as.Date() function to convert a character object with a %m/%d/%Y format to a date object:

#define character object in %m/%d/%Y format
x <- "10/15/2022"

#convert character object to date object
my_date <- as.Date(x, format="%m/%d/%Y")

#view new date object
my_date

[1] "2022-10-15"

#view class of my_date
class(my_date)

[1] "Date"

We can see that the character object has been converted to a date object.

And the following code shows how to use the as.Date() function to convert a character object with a %m%d%Y format to a date object:

#define character object in %m%d%Y format
x <- "10152022"

#convert character object to date object
my_date <- as.Date(x, format="%m%d%Y")

#view new date object
my_date

[1] "2022-10-15"

#view class of my_date
class(my_date)

[1] "Date"

The character object has successfully been converted to a date object.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Convert UNIX Timestamp to Date in R
How to Convert Factor to Date in R
How to Sort a Data Frame by Date in R
How to Extract Year from Date in R

Leave a Reply

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