coolhand
coolhand

Reputation: 2061

Date conversion problems in R

I have a date field in the third column of imported data as a string. I am trying to convert it to a proper date field.

mydata[1,3]
[1] 04/01/1957

The field is initially typed as a factor. I try to convert it to a date with:

mydata$Date <- as.Date(mydata$Date, "%m/%d/%y")

However, this seems to convert incorrectly. The new output is:

mydata[1,3]
[1] "2019-04-01"

Upvotes: 1

Views: 70

Answers (2)

Dirk is no longer here
Dirk is no longer here

Reputation: 368191

It's a duplicate a few dozen times over. anytime::anydate(mydata[1,3]) will do it for you by first converting to character.

Here is your example, made reproducible:

R> dfact <- factor(c("04/01/1957", "05/01/1957", "06/01/1957"))
R> mydata <- data.frame(a=dfact, b=dfact, c=dfact)
R> mydata[1,3]
[1] 04/01/1957
Levels: 04/01/1957 05/01/1957 06/01/1957
R> anytime::anydate(mydata[1,3])
[1] "1957-04-01"
R> 

Note also that your format string is wrong: you have a four-digit year for which you need %Y instead of %y%. And what you (as well as the other answers / comments) missed as well is that as.character() is mandatory here:

R> mydata[1,3]
[1] 04/01/1957
Levels: 04/01/1957 05/01/1957 06/01/1957
R> as.Date(mydata[1,3], "%m/%d/%Y")  # used to be wrong, now works
[1] "1957-04-01"
R> as.Date(as.character(mydata[1,3]), "%m/%d/%Y")  # better but more work
[1] "1957-04-01"
R> anytime::anydate(mydata[1,3])  # easiest
[1] "1957-04-01"
R> 

Edit: I at first overlooked that R now seems to add the as.character() step which used to be mandatory. Small steps. anydate() still helps by allowing us to skip the format string for a fairly large number of possible formats.

Upvotes: 1

jens_laufer
jens_laufer

Reputation: 420

The mistake is that you use %y instead of %Y. %y is a two-digit year and %Y a four digit year.

Check https://www.statmethods.net/input/dates.html

Upvotes: 3

Related Questions