[R] Regular Expression
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Jul 24 20:28:48 CEST 2012
On Tue, Jul 24, 2012 at 1:36 PM, Fred G <bayespokerguy at gmail.com> wrote:
> Hi--
>
> I have three columns in an input file:
> MONTH QUARTER YEAR
> 2012-07 2012-3 2012
> 2001-07 2001-3 2001
> 2002-01 2002-1 2002
>
> I want to make output like so:
> MONTH QUARTER YEAR
> 07 3 2012
> 07 3 2001
> 01 1 2002
>
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.
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list