-2

I am trying to convert a character vector of dates (in the format: i.e. "Jan.1990") to a date vector (keeping a similar format: i.e. "Jan 1990" or "Jan.1990").

month_year <- ("Jan.1990", "Feb.1990", "Mar.1990", "Jan.1991", "Feb.1991", Mar. 1991")

I have tried using as.Date and various commands with the lubridate package but I either end up with an incorrect format or NAs.

I tried using as.Date:

df$month_year <- as.Date(df$month_year,format = "%b%Y")

This resulted in NAs.

I tried lubridate::parse_date_time2

parse_date_time2(tidy_labor$month_year, c("Jan.1990"), exact = TRUE, orders = "bY")

But the format came out as:

unknown timezone 'Jan.1990'unknown timezone 'Jan.1990'   [1] "1990-01-01 GMT" "1990-01-01 GMT" "1990-01-01 GMT" "1990-01-01 GMT" "1990-02-01 GMT" "1990-02-01 GMT" "1990-02-01 GMT"

Any help would be much appreciated.

Zet
  • 9
  • 2

1 Answers1

0

This question comes up a lot and the short answer is that dates require a day.

You could use zoo::as.yearmon:

library(zoo)
as.yearmon("Jan.1990", "%b.%Y")

Or you could assume that the day is always the first of the month, concatenate that to your values and convert to Date type.

neilfws
  • 32,751
  • 5
  • 50
  • 63