Convert numeric to date

17,234

Solution 1

You were setting wrong order of month and date values (in your code was Year, Date, Month, should be Year, Month, Date).

as.Date("20100727", "%Y%m%d")
[1] "2010-07-27"

Solution 2

If it is a numeric value, 20100727, as.Date requires it to be first converted to string, or else, would have issue

as.Date(20100727, "%Y%m%d")

Error in charToDate(x) : character string is not in a standard unambiguous format


anydate from anytime can internally do this conversion

library(anytime)
date1 <- anydate(20100727)
date1
#[1] "2010-07-27"
str(date1)
#Date[1:1], format: "2010-07-27"

Also, takes the character string as input

anydate("20100727")
#[1] "2010-07-27"
Share:
17,234
Ricol
Author by

Ricol

Updated on June 26, 2022

Comments

  • Ricol
    Ricol almost 2 years

    I'd like to convert this type of numeric values to a date but it doesn't work.

    20100727 for instance

    I tried to convert the numeric to character and applied this :

    as.Date("20100727", "Y%d%m")
    

    but it doesn't work.

    How can I do ?