2

I'm trying to convert some dates that are currently set as numeric to a date class. e.g. 19870101

for example:

> as.Date.numeric(19870101, origin = "1970-01-01")
[1] "56417-06-28"

Why does this not return the correct date which should be "1987-01-01". Does anyone know what I am doing wrong here, is the origin argument wrong?

Any ideas? Thanks!

pd441
  • 2,644
  • 9
  • 30
  • 41
  • You can do > lubridate::ymd('19870101') [1] "1987-01-01" > – Karthik S Oct 26 '20 at 09:38
  • 4
    `19870101` is not an actual number here. It is a character date that has been stored as a number. Use `as.Date.character(19870101, format = "%Y%m%d")` (you might need to adjust the format if it follows US conventions). – Roland Oct 26 '20 at 09:40

1 Answers1

5
library(lubridate)
ymd(19870101)

output

[1] "1987-01-01"
Moh
  • 188
  • 8