0

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)
1
  • Note as.Date is base R, not from the lubridate package. You may be thinking of lubridate::as_date() - the lubridate solution would be lubridate::as_date(as.character(Datum)). Also note your last date returns NA since November 31 is not a valid date. Commented Dec 4, 2024 at 14:34

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thanks for helping me out.
3

anydate will convert that:

library(anytime)

anydate(20170911)
## [1] "2017-09-11"

Comments

0
library(lubridate)
ymd(Datum)

Result

[1] "2017-09-11" "2015-11-07" NA    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.