The as.Date() function in R is used to convert various types of date and time objects or character strings into Date objects.
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 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 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.Date(origin, …) to such an object.
- tz: It is a time zone name.
Return Value
It returns the object of class “Date“.
Example 1: Basic usage
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: Converting numeric values to dates
Numeric values can also be converted to dates. By default, numeric conversion assumes the number represents the number of days since 1970-01-01 (the Unix epoch):
# Numeric representation of a date (days since 1970-01-01)
date_numeric <- 19320
# Convert to Date
date_object <- as.Date(date_numeric, origin = "1970-01-01")
# Print the result
print(date_object)
Output
[1] "2022-11-24"
Example 3: Unrecognizable Date Formats
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 4: Setting timezone
To set the timezone, use the tz argument.
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: Returning NA argument
The as.date() returns NA if you have not set the locale properly per your system.
factr <- as.factor("27/05/2022")
date <- as.Date(factr, format = "%Y-%m-%d")
print(factr)
print(date)
Output
[1] 27/05/2022
Levels: 27/05/2022
[1] NA
To handle NA values, convert the factor into POSIXt and 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.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.