How to properly convert, achieve the equivalence of dates

How to properly convert, achieve the equivalence of dates
How to achieve the expected value with the functions to manipulate dates, used in each data.table.
In the case of as.Date, I have varied the origin without achieving the expected result
What to do?
On many occasions I find this numerical format of dates!
the 20200402 format is used on this site

library(data.table)
dt<-as.data.table(list(
                       yr = c(14610, 14975, 15340, 15706, 16071, 16436, 16801, 17167, 
                              17532, 17897, 18262, 18628, 18993, 19358, 19723, 20089,
                              20454, 20819, 21184, 21550, 21915, 22280, 22645, 23011, 
                              23376, 23741, 24106, 24472, 24837, 25202, 25567, 25933, 
                              26298, 26663, 27028, 27394, 27759,  28124, 28489, 28855)))
#help as.Date {base}
## date given as number of days since 1900-01-01 (a date in 1989)
#as.Date(32768, origin = "1900-01-01")
dt[,`:=`(expected=as.IDate(yr),date=as.Date(yr,origin = "1900-01-01")),]
print(dt,topn=3)
      yr   expected       date
 1: 14610 2010-01-01 1940-01-02
 2: 14975 2011-01-01 1941-01-01
 3: 15340 2012-01-01 1942-01-01
---                            
38: 28124 2047-01-01 1977-01-01
39: 28489 2048-01-01 1978-01-01
40: 28855 2049-01-01 1979-01-02

dt2<-as.data.table(list(
  yr = c(20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,
         20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,
         20200327,20200327,20200327)))
dt2[,`:=`(expected=lubridate::ymd(yr),idate=as.IDate(yr),date=as.Date(yr,origin = "1900-01-01")),]
print(dt2,topn=3)
         yr   expected       idate        date
 1: 20200327 2020-03-27 57276-08-13 57206-08-14
 2: 20200327 2020-03-27 57276-08-13 57206-08-14
 3: 20200327 2020-03-27 57276-08-13 57206-08-14
---                                            
19: 20200327 2020-03-27 57276-08-13 57206-08-14
20: 20200327 2020-03-27 57276-08-13 57206-08-14
21: 20200327 2020-03-27 57276-08-13 57206-08-14
 

Try 1970
Or use lubridate? You second example shows you had success using lubridate already.

Do these work for you?

library(data.table)
dt<-as.data.table(list(
  yr = c(14610, 14975, 15340, 15706, 16071, 16436, 16801, 17167, 
         17532, 17897, 18262, 18628, 18993, 19358, 19723, 20089,
         20454, 20819, 21184, 21550, 21915, 22280, 22645, 23011, 
         23376, 23741, 24106, 24472, 24837, 25202, 25567, 25933, 
         26298, 26663, 27028, 27394, 27759,  28124, 28489, 28855)))
dt[,`:=`(expected=as.IDate(yr),date=as.Date(yr,origin = "1970-01-01")),]
print(dt,topn=3)
#>        yr   expected       date
#>  1: 14610 2010-01-01 2010-01-01
#>  2: 14975 2011-01-01 2011-01-01
#>  3: 15340 2012-01-01 2012-01-01
#> ---                            
#> 38: 28124 2047-01-01 2047-01-01
#> 39: 28489 2048-01-01 2048-01-01
#> 40: 28855 2049-01-01 2049-01-01

dt2<-as.data.table(list(
  yr = c(20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,
         20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,
         20200327,20200327,20200327)))
dt2[,`:=`(expected=lubridate::ymd(yr),idate=as.IDate(as.character(yr),format="%Y%m%d"),
          date=as.Date(as.character(yr),format="%Y%m%d")),]
print(dt2,topn=3)
#>           yr   expected      idate       date
#>  1: 20200327 2020-03-27 2020-03-27 2020-03-27
#>  2: 20200327 2020-03-27 2020-03-27 2020-03-27
#>  3: 20200327 2020-03-27 2020-03-27 2020-03-27
#> ---                                          
#> 19: 20200327 2020-03-27 2020-03-27 2020-03-27
#> 20: 20200327 2020-03-27 2020-03-27 2020-03-27
#> 21: 20200327 2020-03-27 2020-03-27 2020-03-27

Created on 2020-04-03 by the reprex package (v0.3.0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.