rlubridateas.date

Converting numeric representation of date to date format in R


I have a dataset with dates in the format like 20170911. Normally with other datasets I work with as.Date from the lubridate package and it works out pretty well. However, when I apply the as.Date function, it does not work out with this dataset. I get a date for the year which is impossible e.g. 57195-09-11 I don't get it, there seems to be a problem with the structure, I guess? Maybe it is because there are no separators included like . or - between the numbers? Can anyone help me out?

As an example

Datum <- c(20170911, 20151107, 20101131)

Solution

  • You should make them characters first and them specify the format of date

    > as.Date(as.character(Datum), format = "%Y%m%d")
    [1] "2017-09-11" "2015-11-07" NA