How to Convert Factor to Date in R

  1. Convert Factor to Date Using the as.Date() Function in R
  2. Convert Factor to Date Using the ymd()function in R
How to Convert Factor to Date in R

We will introduce the method to convert factor to date by using the as.Date() function available in base R library. factor is a data structure that is used to categorize the data and store the categorized data into multiple levels. The levels are represented by the integers. One advantage of using these data structures is that it does not allow duplicate values/features. We can create the dates with the factor function by using the following syntax:

factor(c("string_date",.....................))
#where string_date is the date in the given format "yyyy-mm-dd"

Convert Factor to Date Using the as.Date() Function in R

This function is used to convert the given factor data into date in the given format, and the format should be %Y-%m-%d . Here Y stands for the year to display year in four-digit format, m stands for the month to get the month number, and d stands for day to display the day number.

Here we will create a factor with 5 dates and convert it into date using the above function.

Example Code:

# R
#create factor date with string dates
data = factor(c("2021-11-20","2021-11-19","2021-11-18","2021-11-17","2021-11-16"))

#display
print(data)


#convert string date factor to date using as.Date() function
#in four digit year format
#month and day
final= as.Date(data, format = "%Y-%m-%d")

#display
print(final)

Output:

[1] 2021-11-20 2021-11-19 2021-11-18 2021-11-17 2021-11-16
Levels: 2021-11-16 2021-11-17 2021-11-18 2021-11-19 2021-11-20
[1] "2021-11-20" "2021-11-19" "2021-11-18" "2021-11-17" "2021-11-16"

Convert Factor to Date Using the ymd()function in R

The ymd() function is available in the lubridate library, which transforms the given factor dates into Date or POSIXct objects in the %Y-%m-%d format.

Before using this function, we have to install the lubridate package. This package will deal with and manage date variables.

Let’s see how to install and load the package.

To load the package, we have to use the install keyword, and to load the installed package, we have to use the library keyword.

#Install the package
install("lubridate")

#Load the package
load("lubridate")

Example Code:

#load lubridate library
library("lubridate")

#create factor date with string dates
data = factor(c("2021-11-20","2021-11-19","2021-11-18","2021-11-17","2021-11-16"))

#display
print(data)


#convert string date factor to date using ymd() function
#in four digit year format
#month and day
final= ymd(data, format = "%Y-%m-%d")

#display
print(final)

Output:

[1] 2021-11-20 2021-11-19 2021-11-18 2021-11-17 2021-11-16
Levels: 2021-11-16 2021-11-17 2021-11-18 2021-11-19 2021-11-20
[1] "2021-11-20" "2021-11-19" "2021-11-18" "2021-11-17" "2021-11-16"
Gottumukkala Sravan Kumar avatar Gottumukkala Sravan Kumar avatar

Gottumukkala Sravan Kumar is currently working as Salesforce Developer. During UG (B.tech-Hon's), he published 1400+ Technical Articles. He knows Python, R, Java, C#, PHP, MySQL and Bigdata Frameworks. In free time he loves playing indoor games.

LinkedIn

Related Article - R Factor