The as.Date() function in R converts various types of date and time objects or character strings into Date objects.
as.Date(x,
format,
tryFormats = c("%Y-%m-%d", "%Y/%m/%d"),
optional = FALSE, tz = "UTC",
origin,
...)
Name | Description |
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. |
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"
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"
In the above figure, we took a vector of unrecognizable date formats and converted it into a vector of proper dates.
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"
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”.
In the above figure, we passed the timezone explicitly using the “tz” argument that the output is a proper date.
as.Date("2022/05/27 12:30", tz = "PST8PDT")
# Output: [1] "2022-05-27"
The as.date() function returns NA if the locale has not been set properly according to your system.
The above figure illustrates that if you don’t set the locale properly according to your system, it will return the “NA” value.
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.
Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…
max() The max() function in R finds the maximum value of a vector or data…
The pnorm() function in R calculates the cumulative density function (cdf) value of the normal…
You may want to convert a vector to a string when you need to combine…
Set the current working directory The setwd() function sets the working directory to the new…
The sd() function in R calculates the sample standard deviation of a numeric vector or…