What is the as.Date() function in R?
Overview
The as.Date() function in R converts a date character to an object whose class is Date.
Syntax
as_Date(x)
Parameter value
The as.Date() function takes a single and mandatory parameter value, x, which represents the character representation of a date.
Return value
The as.Date() function returns an object whose class is "Date".
Code example
# creating a data characterdate_in_string <- "2022-01-29"# calling the as.Date() functionmy_date = as.Date(date_in_string)print(my_date)# cheking the classclass(my_date)
Code explanation
- Line 2: We create a data character assigned to the
date_in_stringvariable. - Line 5: We implement the
as.Date()function on thedate_in_stringvariable. The result is assigned to a variable,my_date. - Line 7: We print the variable
my_date. - Line 10: We use the
class()function to check for the class of the object variablemy_date.