Prevent ifelse Function from Turning Dates into Numeric Class in R (2 Examples)

 

This tutorial shows how to avoid that the ifelse function converts dates into numeric objects in the R programming language.

Table of contents:

Let’s jump right to the exemplifying R syntax.

 

Creation of Example Data

Let’s first create some example data in R:

my_dates <- as.Date(c("2025-10-05",                     # Create example dates
                      "2022-01-17",
                      "2023-06-28",
                      "2024-12-22"))
my_dates                                                # Print example dates
# [1] "2025-10-05" "2022-01-17" "2023-06-28" "2024-12-22"

The previous RStudio console output shows that our exemplifying data is a vector of dates.

Let’s apply the ifelse function to these dates:

out_base <- ifelse(my_dates < as.Date("2024-01-01"),    # Apply ifelse function
                   my_dates + 10,
                   my_dates)
out_base                                                # Print output of ifelse function
# [1] 20366 19019 19546 20079

As you can see based on the previous output, our dates have been converted to numbers.

Let’s print the data class of this output:

class(out_base)                                         # Check class of output
# [1] "numeric"

Our output has the numeric class. Kind of unexpected and often not what we want when applying an ifelse statement to dates.

The following examples explain how to avoid this phenomenon by using alternatives to the ifelse function of Base R.

Let’s do this!

 

Example 1: Apply if-else Condition & Keep Date Class Using if_else() Function of dplyr Package

The following R code shows how to use the dplyr package to prevent the transformation of dates to the numeric class when applying an if else statement.

To be able to use the functions of the dplyr package, we first need to install and load dplyr:

install.packages("dplyr")                               # Install dplyr package
library("dplyr")                                        # Load dplyr package

In the next step, we can apply the if_else function (note the underscore) to our vector of dates:

out_dplyr <- if_else(my_dates < as.Date("2024-01-01"),  # Apply if_else function
                     my_dates + 10,
                     my_dates)
out_dplyr                                               # Print output of if_else function
# [1] "2025-10-05" "2022-01-27" "2023-07-08" "2024-12-22"

The previous output contains the expected dates instead of numbers.

Let’s test if this is confirmed by the class of our dplyr output:

class(out_dplyr)                                        # Check class of output
# [1] "Date"

Our output still has the Date class – great!

 

Example 2: Apply if-else Condition & Keep Date Class Using fifelse() Function of data.table Package

In this example, I’ll demonstrate how to apply the functions of the data.table package to apply an ifelse statement to dates and prevent the conversion of these dates to the numeric data type.

First, we need to install and load the data.table add-on package:

install.packages("data.table")                          # Install & load data.table package
library("data.table")

Now, we can apply the fifelse function (note the f at the beginning of ifelse) of the data.table package to our date vector:

out_dtab <- fifelse(my_dates < as.Date("2024-01-01"),   # Apply fifelse function
                    my_dates + 10,
                    my_dates)
out_dtab                                                # Print output of fifelse function
# [1] "2025-10-05" "2022-01-27" "2023-07-08" "2024-12-22"

The output looks the same as in Example 1.

How about the class?

class(out_dtab)                                         # Check class of output
# [1] "Date"

Still the Date class! In other words, the data.table package has returned the same output as the dplyr package.

 

Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which shows the topics of this tutorial. You can find the video below.

 

The YouTube video will be added soon.

 

Additionally, you may read some of the related articles on https://statisticsglobe.com/. You can find a selection of tutorials below:

 

In summary: In this R programming tutorial you have learned how to prevent that the ifelse function turns dates into numeric objects. In case you have further questions, tell me about it in the comments section. Furthermore, please subscribe to my email newsletter for updates on the newest tutorials.

 

Subscribe to the Statistics Globe 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.

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

Menu
Top