'How to change class character to class date in R without getting NA results
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.
Solution 1:[1]
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).
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andrea M |
Solution 2 |