[R-sig-finance] Monthly Sequences

Parlamis Franklin fparlamis at mac.com
Tue Feb 21 21:27:01 CET 2006


On Feb 21, 2006, at 5:26 AM, Gabor Grothendieck wrote:

> There is still a problem with line wrapping.  Check out:
> https://stat.ethz.ch/pipermail/r-sig-finance/2006q1/000679.html
>
> I believe the list will accept text attachments.  To avoid these
> problems just post it as an attachment or if you have web space
> post it with a link.
>
> Also it might be useful if you provide a sentence or two of
> explanation and a few test cases to illustrate how it works.
>


Thanks Gabor, and sorry again for problems.  Attached please find a  
text file with the new function.  Note that I have cleaned this up  
from yesterday's version so please use this one.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: seq.Date.txt
Url: https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20060221/a9044a3a/seq.Date.txt
-------------- next part --------------

When the original seq.Date wants to do yearly or monthly sequences,  
it converts the "from" argument to POSIXlt and manipulates the "year"  
and "mon" vectors, respectively.  Then it converts back with .Internal 
(POSIXlt2Date).

My function adds some appropriate manipulation of the "mday" vector  
in both cases before the .Internal reconversion, if the additional  
"end.end" argument is TRUE (default value is FALSE).  Along the way,  
it includes logic to provide leap-year and last-day-in-month  
information (the leap year logic was published by Diethelm Wuertz in  
this article -- http://www.itp.phys.ethz.ch/econophysics/R/pdf/ 
calendar.pdf).  I have embedded this logic in my code to minimize my  
intrusion on base R, although it could probably be handled in  
external functions that would have other usefulness.  I have also  
employed Gabor's "pmin" logic from the function he posted a week  
back, for which thanks.

I should add that I have no idea how .Internal(POSIXlt2Date) works,  
and I am hoping that no untoward behavior results from my addenda.

Here are some examples.

___

 > seq(as.Date("2005-01-31"), by="month", len=5)
[1] "2005-01-31" "2005-03-03" "2005-03-31" "2005-05-01" "2005-05-31"
 > seq(as.Date("2005-01-31"), by="month", len=5, end.end=TRUE)
[1] "2005-01-31" "2005-02-28" "2005-03-31" "2005-04-30" "2005-05-31"

## Adding the end.end flag caused the month end convention to be  
respected.

 > seq(as.Date("2008-02-28"), by="month", len=5)
[1] "2008-02-28" "2008-03-28" "2008-04-28" "2008-05-28" "2008-06-28"
 > seq(as.Date("2008-02-28"), by="month", len=5, end.end=TRUE)
[1] "2008-02-28" "2008-03-28" "2008-04-28" "2008-05-28" "2008-06-28"
 > seq(as.Date("2008-02-29"), by="month", len=5)
[1] "2008-02-29" "2008-03-29" "2008-04-29" "2008-05-29" "2008-06-29"
 > seq(as.Date("2008-02-29"), by="month", len=5, end.end=TRUE)
[1] "2008-02-29" "2008-03-31" "2008-04-30" "2008-05-31" "2008-06-30"

  ## It knew about leap years.

 > seq(as.Date("2008-01-30"), by="month", len=5)
[1] "2008-01-30" "2008-03-01" "2008-03-30" "2008-04-30" "2008-05-30"
 > seq(as.Date("2008-01-30"), by="month", len=5, end.end=TRUE)
[1] "2008-01-30" "2008-02-29" "2008-03-30" "2008-04-30" "2008-05-30"

## And it handled the case where the "from" date was not end-of-month,
## but exceeded the end-of-month date in later months.

 > seq(as.Date("2005-02-28"), by="year", len=5)
[1] "2005-02-28" "2006-02-28" "2007-02-28" "2008-02-28" "2009-02-28"
 > seq(as.Date("2005-02-28"), by="year", len=5, end.end=TRUE)
[1] "2005-02-28" "2006-02-28" "2007-02-28" "2008-02-29" "2009-02-28"
 > seq(as.Date("2008-02-28"), by="year", len=5, end.end=TRUE)
[1] "2008-02-28" "2009-02-28" "2010-02-28" "2011-02-28" "2012-02-28"
 > seq(as.Date("2008-02-29"), by="year", len=5, end.end=TRUE)
[1] "2008-02-29" "2009-02-28" "2010-02-28" "2011-02-28" "2012-02-29"

## It also handled yearly sequences with February "froms" correctly

___

Franklin


More information about the R-sig-finance mailing list