As Date Function


I just want to simply convert characters to dates for my data frame.

But, instead of correcting the whole range of dates, the as.Date function changes all my dates into either the beginning or last one.

Day <- c("10/10/1988", "14/10/2017")
BetterDates <- as.Date(Day,format = "%d/%m/%y")

This transforms all my dates to 1988-10-10 or 2017-10-14, instead of adjusting the whole range (there's 2000 entries with separate dates).

Any idea what the issue is?

I think you're using an incorrect format. Use %Y to parse years with the full century.

I tried %Y instead and nothing changed :frowning_face:

as.Date(Day, format='%d/%m/%Y')

(Day <- c("10/10/1988", "14/10/2017"))
#[1] "10/10/1988" "14/10/2017"
(BetterDates <- as.Date(Day,format = "%d/%m/%Y"))
#[1] "1988-10-10" "2017-10-14"

Didn't work unfortunately. Still converts my whole string of dates to the two entered.

To exactly what format do you want to convert the dates to?

Do you change Day to be whatever your dates are ?

(Day <- c("10/10/1988", "14/10/1990", "12/12/2012", "14/10/2017"))
#"10/10/1988" "14/10/1990" "12/12/2012" "14/10/2017"
(BetterDates <- as.Date(Day,format = "%d/%m/%Y"))
#"1988-10-10" "1990-10-14" "2012-12-12" "2017-10-14"

I think I'm screwing up just adding it to my data frame?

Because the transformed value is right on inspection.

Min. 1st Qu. Median Mean 3rd Qu.
"1988-10-10" "1996-01-11" "2003-04-13" "2003-04-13" "2010-07-14"
[1] "Date"

This is correct. But then how do I make "BetterDates" replace "Day" in my Cod data frame?

I thought it was just Cod$BetterDates but that keeps breaking it.

assuming you have a dataframe called Cod, (which my first line makes) then you change it like this.

(Cod <- data.frame(old_day = 
                      c("10/10/1988", "14/10/1990",
                       "12/12/2012", "14/10/2017")))

(Cod$BetterDates <- as.Date(Cod$old_day,format = "%d/%m/%Y"))


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.