Priit Mets
Priit Mets

Reputation: 495

How transfrom numeric date to date format

I have a date '2020-01-01', which is transformed to numeric, Thereafter, I want to transform date numeric variable to date variable. How can I do it?

a<-'2020-01-01'
a<-as.numeric(as.Date(a, '%Y-%m-%d'))


#not working
as.Date(a, '%Y%m%d')
format(a, '%Y%m%d')

Upvotes: 1

Views: 79

Answers (2)

akrun
akrun

Reputation: 887901

Using lubridate

library(lubridate)
as_date(a, origin = '1970-01-01')
#[1] "2020-01-01"

Upvotes: 1

GKi
GKi

Reputation: 39727

You have to set origin:

as.Date(a, origin="1970-01-01")
#[1] "2020-01-01"

In ?Date: Dates are represented as the number of days since 1970-01-01, ....

Upvotes: 4

Related Questions