Convert Factor to Date in R (2 Examples)

 

In this tutorial, I’ll show how to change the class of a data object from factor to Date in the R programming language.

The tutorial will consist of these topics:

Let’s start right away:

 

Creation of Example Data

First, we’ll need to create some data that we can use in the exemplifying syntax below:

my_fac <- factor(c("2020-10-05",                   # Create example factor vector
                   "2022-01-07",
                   "2020-10-05",
                   "2023-11-23",
                   "2017-03-11"))
my_fac                                             # Print example factor vector
# [1] 2020-10-05 2022-01-07 2020-10-05 2023-11-23 2017-03-11
# Levels: 2017-03-11 2020-10-05 2022-01-07 2023-11-23

As you can see based on the previous output of the RStudio console, our example data is a vector consisting of five elements.

Each of the elements contains a date. However, the vector has the factor class.

The following examples show two different alternatives on how to convert factors to the class Date in R.

 

Example 1: Converting Factor to Date Using as.Date() Function

In this example, I’ll show how to use the as.Date function to convert factors to the data type Date.

It is important to specify the format argument within the as.Date function. Note that the way how you have to specify the format argument depends on the structure of your dates (i.e. our factor has the structure Year-Month-Day).

Have a look at the following R code and its output:

my_dates1 <- as.Date(my_fac, format = "%Y-%m-%d")  # Applying as.Date function
my_dates1                                          # Printing output to RStudio console
# [1] "2020-10-05" "2022-01-07" "2020-10-05" "2023-11-23" "2017-03-11"

The previous R syntax created a new data object called my_dates1.

Let’s check the class of this new data object using the class function:

class(my_dates1)                                   # Checking class
# [1] "Date"

Our new data object has the class Date – Perfect!

 

Example 2: Converting Factor to Date Using ymd() Function of lubridate Package

It is also possible to use the functions of the lubridate package to convert factors to dates in R.

First, we have to install and load the lubridate package to R:

install.packages("lubridate")                      # Install & load lubridate
library("lubridate")

Now, we can apply the ymd function provided by the lubridate package:

my_dates2 <- ymd(my_fac)                           # Applying ymd function
my_dates2                                          # Printing output to RStudio console
# [1] "2020-10-05" "2022-01-07" "2020-10-05" "2023-11-23" "2017-03-11"

The previous R code created a new vector object called my_dates2. Let’s check the class:

class(my_dates2)                                   # Checking class
# [1] "Date"

Our new data object my_dates2 also has the Date class. Note that depending on the structure of your input data, the resulting class of the lubridate function might also be the POSIXct class.

 

Video & Further Resources

I have recently released a video on the Statistics Globe YouTube channel, which illustrates the R programming codes of this post. You can find the video below.

 

The YouTube video will be added soon.

 

Furthermore, you might want to have a look at the related tutorials on this website.

 

In summary: In this tutorial, I illustrated how to switch data types from factor to Date in R. If you have further questions, don’t hesitate to let me know in the comments section below.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top