Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert number of days since Jan 1 2000 into date format

I have a column in my data as a number of days since Jan 1, 2000 (this is 0 day). How can I convert day number into date format (2000-01-01)?

Number_of_days    Date
3536              2011-01-03
3537              2011-01-04
3538              2011-01-05
3539              2011-01-06
3540              2011-01-07
3541              2011-01-08

I want to add a new column called Date similar to above one into my df. Number of days to Date above is just an example.

like image 314
Zmnako Awrahman Avatar asked Oct 27 '17 07:10

Zmnako Awrahman


1 Answers

You need the origin-parameter of as.Date. Using:

as.Date(3536:3541, origin = '2000-01-01')

gives:

[1] "2009-09-06" "2009-09-07" "2009-09-08" "2009-09-09" "2009-09-10" "2009-09-11"

In your case you can use:

as.Date(df$Number_of_days - 1, origin = '2000-01-01')

or:

as.Date(df$Number_of_days, origin = '1999-12-31')
like image 59
Jaap Avatar answered Sep 30 '22 11:09

Jaap