Convert numeric to date

前端 未结 2 533

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: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"
    

提交回复
热议问题