Home > Software engineering >  Cannot convert date from Jan 11, 2002 to 2002-01-11 in R
Cannot convert date from Jan 11, 2002 to 2002-01-11 in R

Time:12-27

I'm trying to change the date format in R. I have a data frame and one of the columns contains dates (as strings) in given format: Jan 11, 2002 but I would like to change the format to (also as string): 2002-01-11

I have tried many things, but nothing seems to work. My best shot was trying to convert it to Data object and then convert it back to string, but in different format.

Here is a piece of my code: df$date = strftime(as.Date(df$date, format="%b %d, %Y"), "%Y-%m-%d")

I was trying other ways, but the result is always NA or a string, but in 'old' format.

I think there is something wrong with the first format: "%b %d, %Y", because when I tried the same thing but with different input, e.g. 11/01/2002 ("%d/%m/%Y") everything worked just fine.

I'm pretty new to R so any help would be appreciated.

CodePudding user response:

This would need a reproducible example for an accurate response.

First, confirm that the source dates are all in the same format, otherwise, this can lead to parsing errors.

Try using one of the following:

# Option 1: as.Date()
df <- df %>% mutate(strftime = as.Date(strftime))
# Option 2: lubridate::mdy (note, this can be any order depending on your data, e.g. mdy, dmy, ymd, etc. so is flexible
df <- df %>% mutate(strftime = lubridate::mdy(strftime))

Please do look into reproducible examples to get the best answers.

CodePudding user response:

There are a number of packages that can help (as well as base R functions, see the first comment). Here is my favourite solution which does require any input format help:

> library(anytime)      # for function anydate()
> input <- "Jan 11, 2002"
> d <- anydate(input)
> d
[1] "2002-01-11"
> 

CodePudding user response:

Maybe you wrote it wrong, see, you've written:

df$date = strftime(as.Date(df$date, format="%b %d, %Y"), "%Y-%m-%d")

should be:

df$date = strftime(as.Date(df$date, format="%m %d, %Y"), "%Y-%m-%d")

in format() there's a '%b' and I think it should be '%m'

  • Related