as.Date in R: How to Use as.Date() Function

To get today’s date in R, use the Sys.Date() function.

Sys.Date()

Output

[1] "2021-03-11"

To get the current date and time in R, use the date() function.

date()

Output

[1] "Thu Mar 11 16:59:16 2021"

Dates can be imported from character, numeric, POSIXlt, and POSIXct formats in R using as.Date() method.

as.Date in R

The as.Date() is a built-in R function that converts between character representations and class “Date” objects representing the calendar dates. Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates.

Syntax

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

Parameters

x: It is an object to be converted.

format: It is a character string. If the format argument is not specified, it will try tryFormats one by one on the first non-NA element and give an error if none works. Otherwise, the processing is via strptime.

tryFormats: It is a character vector of format strings to try if the format is not specified.

optional: It is a logical argument indicating to return NA (instead of signaling an error) if the format guessing fails.

origin: It is a Date object or something that can be coerced by.Date(origin, …) to such an object.

tz: It is a time zone name.

Return Value

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

Example

Let’s get the locale-specific date in R, using format() and Sys.Date() function.

dates <- format(Sys.Date(), "%a %b %d")
dates

Output

[1] "Thu Mar 11"

To convert a string to date in R, use the as.Date() function.

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"

You can see that we used the as.Date() function to convert character data to dates. The format is as.Date(x, “format“), where x is the character data and format gives the appropriate format.

Convert date format using as.Date() function

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

Let’s define a vector of dates in one format, and then we will use the as.Date() function to convert it into another format.

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"

How to Convert Date to Character in R

To convert a Date to a Character in R, use the as.Character( ) function.

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

Output

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

R as.Date() origin

We can pass the origin, which can be coerced by as.Date(origin) to such an object.

Let’s import Dates from the Numeric Format.

as.Date(34291, origin = "1900-01-01")

Output

[1] "1993-11-20"

The output shows the dates in the ISO 8601 international standard format %Y-%m-%d.

R as.Date timezone

To use the timezone in as.Date() function in R, pass the tz argument. To get the correct date in your timezone, you need to tell as.Date() function timezone explicitly or if your computer system’s time is set to PST8PDT, you can just 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"

You can see that my default timezone is not PST8PDT, so I pass the timezone explicitly.

How to Change Date Formats in R

To change the date formats in R, use the format() function. For example, if you want to get the date than the standard %Y-%m-%d, use the format() function from the base package.

dates <- c("11/20/80", "11/20/91", "11/20/1993", "09/10/93")
dt <- as.Date(dates, "%m/%d/%y")
dt
cat("After formatting a date in other format", "\n")
fmt <- format(dt, "%a %b %d")
fmt

Output

[1] "1980-11-20" "1991-11-20" "2019-11-20" "1993-09-10"
After formatting a date in other format
[1] "Thu Nov 20" "Wed Nov 20" "Wed Nov 20" "Fri Sep 10"

R as.date() returns NA

The as.date() function returns NA if you have not set the locale properly as 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.

See the following code example.

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 resolve the NA issue, we can convert the factor into POSIXt and then convert it 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"

You can see that now as.Date() function does not return the NA value. If you are dealing with an external CSV dataset and as.Date() function returns NA values, namely, when the Date variable is read in factor format.

data <- read.csv("sample.csv", stringsAsFactors = FALSE)
data$date <- as.Date(data$date)

The above solution might not work for every problem that results in as.Date() returning NA’s, but it does work for some problems.

Final words

The format() and as.character() are built-in R methods that return a character vector representing the date. The as.Date() method returns the object of class “Date”.

That is it for as.Date() function in R tutorial.

Related posts

How to format dates in R

How to Convert Date to Numeric Format in R

Leave a Comment