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 character
date_in_string <- "2022-01-29"
# calling the as.Date() function
my_date = as.Date(date_in_string)
print(my_date)
# cheking the class
class(my_date)

Code explanation

  • Line 2: We create a data character assigned to the date_in_string variable.
  • Line 5: We implement the as.Date() function on the date_in_string variable. 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 variable my_date.

Free Resources