Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting a Year-quarter string to date object in R

I have below string

dat = '2008Q1'

I want to convert this to a date object, so I tried below,

as.Date(dat, format = '%Y%Q')

This gives

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

NA

In python, there is a direct method as stated in How to Convert a specialty date string to a datetime object

Is there any function available in R to do the same?

>Solution :

Simplest with {lubridate}

library(lubridate)

yq(dat)
#[1] "2008-01-01"

Alternative with {zoo}

library(zoo)

as.Date(as.yearqtr(dat, format = "%YQ%q"))

#[1] "2008-01-01"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading