-1

I have a variable in the right format to delineate Year-Quarter (ie "2015 Q1") however, it is stored in R as a character and I am trying to complete a time-series analysis that requires I have dates. My data is collected quarterly, hence the quarter format.

I have tried using the lubridate and zoo packages to convert the character variable into a recognizable date.

str(Qtotal)
'data.frame':   24 obs. of  5 variables:
 $ Quarter  : chr  "2017 Q1" "2017 Q2" "2017 Q3" "2017 Q4" ...
 $ date     : Date, format: NA NA NA ...

Qtotal$date <- as.Date(Qtotal$Quarter, format = "%m-%d-%y")
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

Other things I've tried:

total$date<-as.Date(parse_date_time(Qtotal$Quarter, c('mdy')))

Warning message: All formats failed to parse. No formats found.

Qtotal$date<-as.Date(parse_date_time(Qtotal$create_date, c('mdy')))

Error in $<-.data.frame(*tmp*, date, value = numeric(0)) : replacement has 0 rows, data has 24

Sotos
  • 51,121
  • 6
  • 32
  • 66
hannahamdi
  • 13
  • 1

1 Answers1

2

We can convert to 'yearqtr' class with as.yearqtr (from zoo) and coerce it to Date

library(zoo)
as.Date(as.yearqtr(Qtotal$Quarter))
akrun
  • 874,273
  • 37
  • 540
  • 662