How to Convert Numbers to Dates in R


Often you may need to convert numbers to date formats in R. The easiest way to do this is by using the lubridate package, which has several helpful functions for dealing with dates in R.

This tutorial provides several examples of how to use these functions in practice.

Example 1: Convert Integers to Dates

The following code shows how to convert a column of integer values in a data frame to a date format by using the ymd() function:

library(lubridate)

#create data frame
df <- data.frame(date = c(20201022, 20201023, 20201026, 20201027, 20201028),
                 sales = c(4, 7, 8, 9, 12))

#convert date column from numeric to year-month-date format
df$date <- ymd(df$date)

#view data frame
df

        date sales
1 2020-10-22     4
2 2020-10-23     7
3 2020-10-26     8
4 2020-10-27     9
5 2020-10-28    12

#view class of date column
class(df$date)

[1] "Date"

Note that the lubridate package has several functions to handle different date formats.

For example, the following shows how to convert a column of integer values in a data frame to a date format by using the ydm() function:

library(lubridate)

#create data frame
df <- data.frame(date = c(20202210, 20202310, 20202610, 20202710, 20202810),
                 sales = c(4, 7, 8, 9, 12))

#convert date column from numeric to year-month-date format
df$date <- ydm(df$date)

#view data frame
df

        date sales
1 2020-10-22     4
2 2020-10-23     7
3 2020-10-26     8
4 2020-10-27     9
5 2020-10-28    12

#view class of date column
class(df$date)

[1] "Date"

Example 2: Convert Months & Years to Dates

The following code shows how to convert a column of numeric values that represent the number of months from January 1st, 2010 to a date format by using the months() function:

library(lubridate)

#create data frame
df <- data.frame(date = c(11, 15, 18, 22, 24),
                 sales = c(4, 7, 8, 9, 12))

#convert date column from numeric to year-month-date format
df$date <- as.Date('2010-01-01') + months(df$date)

#view data frame
df

        date  sales
1 2010-12-01      4
2 2011-04-01      7
3 2011-07-01      8
4 2011-11-01      9
5 2012-01-01     12

#view class of date column
class(df$date)

[1] "Date"

And the following code shows how to convert a column of numeric values that represent the number of years from January 1st, 2010 to a date format by using the years() function:

library(lubridate)

#create data frame
df <- data.frame(date = c(11, 15, 18, 22, 24),
                 sales = c(4, 7, 8, 9, 12))

#convert date column from numeric to year-month-date format
df$date <- as.Date('2010-01-01') + years(df$date)

#view data frame
df

        date  sales
1 2021-01-01      4
2 2025-01-01      7
3 2028-01-01      8
4 2032-01-01      9
5 2034-01-01     12

#view class of date column
class(df$date)

[1] "Date"

Bonus: Refer to this cheat sheet to gain a better understanding of the functions available in the lubridate package.

Leave a Reply

Your email address will not be published.