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

Rui Barradas ruipbarradas at sapo.pt
Fri Nov 9 10:48:03 CET 2012


Hello,

Something like this?


fun <- function(years, periods){
     res <- lapply(splitIt(1:12, periods), function(x) 
groupingStrings(years, x))
     names(res) <- as.integer(names(res)) + 1L
     res
}

fun(2010:2011, 1)
fun(2010:2011, 2)
fun(2010:2011, 4)
fun(2010:2011, 12)


Hope this helps,

Rui Barradas
Em 09-11-2012 03:39, Keith Weintraub escreveu:
> 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