Search code examples
rdateas.date

%b-%Y date conversion gives NA


I am trying to convert character strings to Dates in R. These are examples of the character strings:

"Aug-1973" "Aug-1974" "Aug-1975" "Aug-1976" "Aug-1977"

I run the following line on date strings similar to the ones above:

exportsDF$Date <- as.Date(as.character(exportsDF$Date), format = "%b-%Y")

This returns NAs for all values. The step where I convert the dates column to characters returns the correct values. Any ideas why the as.Date() command is not working? There are no NAs or missing values in the data. Every value has a "%b-%Y" format.

Any help is appreciated!


Solution

  • The date format needs a day as well, so you could add an arbitrary day of the month. Here, I've chosen the first day:

    dates <- c("Aug-1973", "Aug-1974", "Aug-1975", "Aug-1976", "Aug-1977")
    res <- as.Date(paste0("01-", dates), format = "%d-%b-%Y")
    print(res)
    #[1] "1973-08-01" "1974-08-01" "1975-08-01" "1976-08-01" "1977-08-01"
    

    The reason is that the underlying Date data type is an integer counting the days since some reference day. Specifically, the number of days since 1970-01-01. See ?Date.

    The Date object res can now be displayed as you please via

    format(res, "%B-%Y")
    #[1] "August-1973" "August-1974" "August-1975" "August-1976" "August-1977"
    

    or similar.

    The month(res) function and its cousins are also helpful. See ?month.