Home > OS >  Change date variable format in R
Change date variable format in R

Time:12-20

I have a dataset with a date variable:

2004/11/23 2022/8/1

So I rewrote them like this:

2004-11-23 2022-8-1

At this point class() returns "character". I have a problem converting this to "Date".

I tried:

df <- df %>% 
  mutate(
    # rewrite / as -
    date = gsub("/", "-", date)
  ) %>% 
  as.Date()

But the following error appreared:

Error in as.Date.default(.) : 
  do not know how to convert '.' to class “Date”

As far as I see, my rewritten variable (2004-11-23) does not have '.' Could anybody tell what is wrong with my code?

CodePudding user response:

Without knowing much about your data, I think you're going to need to do this instead:


df <- df %>% 
  mutate(
    # rewrite / as -
    date = gsub("/", "-", date) %>% as.Date()
  ) 

CodePudding user response:

There appear to be some data in date which do not match the expected format %Y/%m/%d. You could use grepl to flush out such bad values:

data[!grepl("^\\d{4}/\\d{1,2}/\\d{1,2]$")]

This is not a full solution, because how you handle anything returned by the above one-liner is up to you. But at least this lets you identify the problematical data.

  • Related