6 C
London
Tuesday, March 11, 2025
HomeStatistics TutorialRHow to Convert Numbers to Dates in R

How to Convert Numbers to Dates in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 #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 #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 #convert date column from numeric to year-month-date format
df$date 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 #convert date column from numeric to year-month-date format
df$date 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.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories