0
votes

I have a set of dates formatted as following:

datevalue <- 20130118

I want to format them to a proper date and I try to do this with the following code:

date <- as.Date(datevalue,"%Y%m%d")

This is very similar to the example listed in the documentation but for some reason not working. In the documentation the following example is listed:

dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")
as.Date(dates, "%m/%d/%y")
[1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28"
[5] "1992-02-01"

However, I get the following error message: "Error in charToDate(x): character string is not in a standard unambiguous format". This makes at some level sense to me, but I have the feeling that my syntax defined it properly. Anybody who can explain why the code is not working?

1

1 Answers

2
votes

Defining the variable as a character (as mentioned in @Arun's comment) worked. Code:

date2 <- as.Date(as.character(datevalue),"%Y%m%d")