R date conversion(from character to numeric)

I have a dataframe column containing character dates ( it is a SAS dataset that I'm reading in R) with formats such as "27JUN2022" "28JUN2022" "29JUN2022" "30JUN2022" "01JUL2022" "02JUL2022" "03JUL2022" etc.(equivalent to date9. format of SAS). when I convert it into numeric dates by mutate(as.Date( dataframe$variable, "%Y-%m-%d"), it gives NA as result. I have checked there is no NA in the column.

Is there any possible solution exists? the desired output is to simply convert the column in to numeric date.

Thank you very much in advance.

Your format "%Y-%m-%d" does not match the format of the data 27JUN2022

I usually do it this way

(the_date_strings <- c("27JUN2022","28JUN2022","29JUN2022","30JUN2022","01JUL2022","02JUL2022","03JUL2022"))
#> [1] "27JUN2022" "28JUN2022" "29JUN2022" "30JUN2022" "01JUL2022" "02JUL2022"
#> [7] "03JUL2022"
(the_date_dates <- lubridate::dmy(the_date_strings))
#> [1] "2022-06-27" "2022-06-28" "2022-06-29" "2022-06-30" "2022-07-01"
#> [6] "2022-07-02" "2022-07-03"
#> [1] "Date"

Created on 2023-01-17 with reprex v2.0.2

thanks, but may I know what's the right format here, I did try with %d%m%y, but no help. can you suggest the right format to be used something similar to DDMMMYYYY?

if you look at the help file for strptime you will see info on the formatter syntax.


this include thats %y is 2 digit year; the year without century; you would want %Y which is 4 digits.

Thanks @technocrat, is there any function to get the numeric dates in to the exact same format the way it is in character.
I did try mdy() , but I got NA.

technocrat said you use dmy. not mdy. dmy works. lubridate usually very slow in my limited experience
And if you follow the help to strptime (as suggested) you will see the different formats. You wanted:


Thank you mike. yes, I did check and dmy worked as per the suggestion by @technocrat.

Keep in mind that an object of class Date has both an internal aspect and a display aspect. @mikecrobp provides the level of control that you are looking for here.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.