What is the as.Date() Function in R

The as.Date() function in R is “used to convert a character objects to date objects. To convert a character to the date class in R, you can use the “as.Date()” function.

Syntax

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

Parameters

  1. x: It is an object to be converted.
  2. format: It is a character string. If the format argument is not specified, it will tryFormats one by one on the first non-NA element and give an error if none works. Otherwise, the processing is via strptime.
  3. tryFormats: It is a character vector of format strings to try if the format is not specified.
  4. optional: It is a logical argument to return NA (instead of signaling an error) if the format guessing fails.
  5. origin: It is a Date object or something that can be coerced.Date(origin, …) to such an object.
  6. tz: It is a time zone name.

Return Value

The as.Date() method returns the object of class “Date“.

Example 1: Converting string to date using as.Date()

To convert a string to date in R, use the “as.Date()” function. To use the as.Date() function, you have to provide a character vector representing a date in a format that an R can understand.

dates <- c("20nov1980", "20nov1991", "20nov1993", "10sep1993")
dt <- as.Date(dates, "%d%b%Y")
dt

Output

[1] "1980-11-20" "1991-11-20" "1993-11-20" "1993-09-10"

Example 2: Convert date format using as.Date() function

If your dates are stored as characters, you must provide them as.Date() function with your vector of dates and the format they are currently stored in, and it is done.

dates <- c("11/20/80", "11/20/91", "11/20/1993", "09/10/93")
dt <- as.Date(dates, "%m/%d/%y")
dt

Output

[1] "1980-11-20" "1991-11-20" "2019-11-20" "1993-09-10"

Example 3: as.Date() function with “timezone” parameter

To use the timezone in as.Date() function in R, pass the tz argument. You need to tell as to get the correct date in your timezone.Date() function timezone explicitly, or if your computer system’s time is set to PST8PDT, you can use the tz=” ” argument, which means “use the current system time zone”.

as.Date("2022/05/27 12:30", tz = "PST8PDT")

Output

[1] "2022-05-27"

Example 4: as.date() function returns NA

The as.date() function returns NA if you have not set the locale properly per your system. The NA value is returned when you try to convert your date of class factor into a date of class Date.

First, you have to convert into POSIXt format; otherwise, as.Date() function doesn’t know what part of your string corresponds to what.

a <- as.factor("27/05/2022")
b <- as.Date(a, format = "%Y-%m-%d")
print(a)
print(b)

Output

[1] 27/05/2022
Levels: 27/05/2022
[1] NA

To fix the NA issue, we can convert the factor into POSIXt and then into a date.

a <- as.factor("27/05/2022")
pos_dt <- strptime(a, format = "%d/%m/%Y")
b <- as.Date(pos_dt, format = "%Y-%m-%d")
print(a)
print(b)

Output

[1] 27/05/2022
Levels: 27/05/2022
[1] "2022-05-27"

That’s it.

Leave a Comment