Home > Mobile >  How to change class character to class date in R without getting NA results
How to change class character to class date in R without getting NA results

Time:05-14

I've got a data table with a column of dates: ex_dates <- c("2022-01-01", "2022-05-28") they are now at class character, how can I change them to class date?

I've tried this code:

data$date <-  as.Date(data$date,"%d/%m/%Y")

but it changes the whole column to NA.

CodePudding user response:

If you want to use base R, you need to specify the correct format.

%d/%m/%Y is expecting dates in the following format: 01/12/2020

But in your example, years come before months and before days, and these are separated by a -, not a /. So you need to change to the following:

data$date <- c("2022-01-01", "2022-05-28")
data$date <-  as.Date(data$date, "%Y-%m-%d")

Because this is the standard format, you could also avoid specifying it:

data$date <-  as.Date(data$date)

(Personally I always use lubridate as it's much easier).

CodePudding user response:

The as.Date() function expects your actual date format.

Your ex_dates are %Y-%m-%d not "%d/%m/%Y" thats why you got NA's

The correct way would be

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

I see alot of peaple who thinks the format argument in as.Date() is a transformating parameter. But no, its just yout helping the as.Date() to recognize the date aspects on your vector.

CodePudding user response:

You can use lubridate package with the function YMD:

class(data$date)
[1] "character"

install.packages("lubridate")
library("lubridate")

data$date <- ymd(data$date)
class(data$date)
[1] "Date"

UPDATE without lubridate

 data$date <- as.Date(data$date, format = "%Y-%m-%d")
  •  Tags:  
  • r
  • Related