How can numbers be converted to dates in R?

In R, numbers can be converted to dates by using the as.Date() function. This function takes in a numeric value and converts it into a date format, following the standard format of “yyyy-mm-dd”. The as.Date() function also allows for the specification of the input format, making it easier to convert numbers in different date formats. Additionally, the lubridate package in R provides various functions for converting numbers to dates, with added features such as handling time zones and leap years. Overall, the process of converting numbers to dates in R is straightforward and can be easily customized to fit specific date formats or requirements.

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 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 to gain a better understanding of the functions available in the lubridate package.

x