VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-convert-numbers-to-dates-in-r/

⇱ How to Convert Numbers to Dates in R? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Convert Numbers to Dates in R?

Last Updated : 28 Dec, 2021

In this article, we will discuss how to convert Numbers to Dates in R programming language.

Method 1: Convert Integer to Date Using as.Date() & as.character() Functions

Here we have to consider an integer and then first we have to convert that integer into a character using as.character() function and then convert that character using as.Date() function and finally convert into date using "%Y%m%d" format

Syntax:

 as.Date(as.character(integer),format = "%Y%m%d")

where, an integer is an input number

Example: Convert Numbers to date

 Output: 

[1] "2021-11-02"
[1] "2020-12-09"

Method 2 : Convert Integer to Date Using strptime() Function

strptime() is used to convert into the date from an integer in the "%Y%m%d" format

Syntax:

strptime(integer, format = "%Y%m%d")

Example: Convert Numbers to date

 Output:

[1] "2021-11-02 UTC"
[1] "2020-12-09 UTC"

Method 3: Convert Integer to Date Using ymd() Function of lubridate Package

Here ymd() is used to convert the integer into year, month, and date which is available in lubridate() package 

Syntax:

ymd(integer)

Example: Convert Numbers to date

 Output:

[1] "202-11-12"
[1] "2020-12-09"
Comment
Article Tags:

Explore