Home > OS >  Converting a Year-quarter string to date object in R
Converting a Year-quarter string to date object in R

Time:03-24

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

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?

CodePudding user response:

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"

  • Related