Convert numeric to date

前端 未结 2 522

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 charact

相关标签:
2条回答
  • 2020-12-03 23:13

    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"
    
    0 讨论(0)
  • 2020-12-03 23:22

    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"
    
    0 讨论(0)
提交回复
热议问题