Skip to content
Prev 301109 / 398502 Next

Regular Expression

On Tue, Jul 24, 2012 at 1:36 PM, Fred G <bayespokerguy at gmail.com> wrote:
Normally there is no need to store components of the date.  Its
usually easier to just extract what you need on the fly.  Since you
only seem to need the year, quarter and month if DF is your data frame
you can store the date as a yearmon class object which is rich enough
to contain everything else so you don't really need the MONTH, QUARTER
and YEAR columns making everything simpler.

library(zoo)
ym <- as.yearmon(DF$MONTH)

Now the year, quarter and month are:

floor(ym)
format(as.yearqtr(ym), "%q")
format(ym, "%m")

The last two return character strings which is likely ok but if you
need them as numeric then use as.numeric(format(ym, "%m")) and
similarly for the quarter.

This does not involve regular expressions or intricate character manipulation.