I am trying to create a new column denoting quarter in my data.frame
using an ifelse
statement on a date column of class as.POSIXct
.
this is a short verion of the date
a
spot.id local.date
q12014local.1 11824267 2013-12-30
q12014local.2 11825708 2013-12-30
q12014local.3 11823669 2013-12-30
q12014local.4 11825407 2013-12-30
q12014local.5 11824268 2013-12-30
q12014local.6 11825709 2013-12-30
q12014local.7 11823670 2013-12-30
q12014local.8 11825408 2013-12-30
q12014local.9 11824266 2013-12-31
q12014local.10 11825707 2013-12-31
This is the ifelse
statement i wrote:
> a$quarter <- ifelse(a$local.date >= 2013-07-01 & a$local.date <= 2013-09-30,"q32013",
ifelse(a$local.date >= 2013-10-01 & a$local.date <= 2013-12-31 , "q42013",
ifelse(a$local.date >= 2014-01-01 & a$local.date <= 2014-03-31, "q12014",
ifelse(a$local.date >= 2014-04-01 & a$local.date <= 2014-06-30, "q22014", ifelse(a$local.date >= 2014-07-01 & a$local.date <= 2014-09-30, "q32014", NA)))))
For some reason, all I get is NA values in my new column ! I can use the row.names in my data.frame
with a gsub
like a$quarter <- gsub("[\\.][0-9]+", "", row.names(a))
but that's not ideal. If ifelse
is unclassing date objects, I tried using the function from this post : How to prevent ifelse() from turning Date objects into numeric objects but that didn't work out. I still get NA values. What's the right way to achieve my objective ?
EDIT: Was unaware of the numerous functions like quarters
, format.yearqtr
available in zoo
and lubridate
. But related to the same, what if my quarters are not the same as calendar year quarters - For e.g. My quarter 1 begins in October, quarter 2 in Jan etc.