[R] Creating yyyymm regexp strings on the fly for aggregation.

arun smartpink111 at yahoo.com
Fri Nov 9 05:32:10 CET 2012


HI,

Not sure whether you are looking for something similar to this:
I modified groupingStrings():
groupingStrings<-function(yrs,numSplits) unlist(lapply(yrs,function(x) paste(x,formatC(numSplits,width=2,flag=0),sep="")))
splitIt <- function(x, n) {split(x, sort(rank(x) %% n))}


lapply(split(groupingStrings(2004:2006,1:12),gsub("(^\\d{4}).*","\\1",groupingStrings(2004:2006,1:12))),function(x) splitIt(x,4))
$`2004`
$`2004`$`0`
[1] "200401" "200402" "200403"

$`2004`$`1`
[1] "200404" "200405" "200406"

$`2004`$`2`
[1] "200407" "200408" "200409"

$`2004`$`3`
[1] "200410" "200411" "200412"
.........................................
..............................................

A.K.





----- Original Message -----
From: Keith Weintraub <kw1958 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Thursday, November 8, 2012 10:39 PM
Subject: [R] Creating yyyymm regexp strings on the fly for aggregation.

Folks,
  This question is somewhat related to a previous posting of mine.

I just can't seem to create a generic solution.

Here is a function that I found searching around the internet:
splitIt <- function(x, n) {split(x, sort(rank(x) %% n))}

I use it like so:
> splitIt(1:12, 2)
$`0`
[1] 1 2 3 4 5 6

$`1`
[1]  7  8  9 10 11 12

Or
> splitIt(1:12, 4)
$`0`
[1] 1 2 3

$`1`
[1] 4 5 6

$`2`
[1] 7 8 9

$`3`
[1] 10 11 12

I am splitting 12 months into 6-month or quarterly chunks. I can also use the function to create monthly or segments or one big annual segment.

Here is a function that I developed:

groupingStrings<-function(yrs, numSplits) {
unlist(lapply(yrs, function(x){ paste(x,formatC(numSplits, width = 2, flag = 0), collapse = "|", sep = "")}))
}

Here is an example of running the function:
groupingStrings(2004:2006, 1:3)
[1] "200401|200402|200403" "200501|200502|200503" "200601|200602|200603"

This would yield first quarter matches for the years 2004 through 2006.

My plan was to use both splitIt and groupingStrings to be able to create regexps  all quarters.

In addition I want it to be flexible enough for me to be able to create matching regexps for monthly, quarterly, semi-annually and annual regexps.

One more example. Suppose I wanted to look at data semi-annually for 2010 through 2011. The regexps would be:
"201001|201002|201003|201004|201005|201006"
"201007|201008|201009|201010|201011|201012"
"201101|201102|201103|201104|201105|201106"
"201107|201108|201109|201110|201111|201112"

I hope I have explained my problem clearly.

Thanks much for your time,
KW


--


    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list