Search code examples
rdateformatdate-conversion

Converting character to date R


I am running into some date issues when working with Dates in R.

Here's my situation.

I'm working on a dataset with a column date (ProjectDate) having the following values

class(Dataset$ProjectDate)
"character"

head(Dataset$ProjectDate)
"End July 2014" "End August 2014" "End September 2014" "End October 2014"

I would like to convert it to "%M %Y" format

How can I do that ?

Thanks


Solution

  • You should think of using 2 step process. First remove the End part from the ProjectDate using sub.

    Now you can apply yearmon from zoo library to convert to month year date format.

    library(zoo)
    
    as.yearmon(sub("^End ", "", df$ProjectDate), "%b %Y")
    #[1] "Aug 2014" "Sep 2014"