In Order to type cast to date in R we will be using as.Date() function. Let’s see how to type cast a text to date in R or factor to date in R with an example using as.date function.
Let’s first create the dataframe.
### Create Data Frame df1 = data.frame ( Name =c('Annie','Catherine','Teresa','Peterson','Richard','Joe'), Date_of_birth =c('1995-06-16','1991-04-19','1993-07-22','1990-03-26','1991-05-12','1992-09-13')) df1
So the resultant dataframe will be
Let’s check the current data type of date_of_birth column.
# Get data type of column sapply(df1,class)
So the data types will be
Type cast to date in R:
Date_of_birth column is type casted using as.Date function as shown below
# Type cast the column to date df1$Date_of_birth <- as.Date(df1$Date_of_birth) sapply(df1,class)
As the result the Date_of_birth column is type casted to date .
Result: