Problems with monthly sequences

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems with monthly sequences

Wojciech Slusarski
Dear R users,

Could anyone help me with creating a monthly series of dates?
When I try to do:

dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")

I get:

> dates
[1] "Europe/Warsaw"
 [1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
 [6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
[11] [2006-10-31] [2006-12-01] [2006-12-31]

Unfourtunately, I would like to heave end of month at each point.
I thought, that maybe using this function would also help when trying to add
a month to a date. I would like to have

"2006-01-31" + 1M = "2006-02-28"

in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
couldn't find any similar function in R for that. Am I missing some
function?

Best regards,
Wojtek

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Gabor Grothendieck
Try this:

library(fCalendar)
dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
dates <- timeLastDayInMonth(dates)


On 2/4/06, Wojciech Slusarski <[hidden email]> wrote:

> Dear R users,
>
> Could anyone help me with creating a monthly series of dates?
> When I try to do:
>
> dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
> "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
>
> I get:
>
> > dates
> [1] "Europe/Warsaw"
>  [1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
>  [6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
> [11] [2006-10-31] [2006-12-01] [2006-12-31]
>
> Unfourtunately, I would like to heave end of month at each point.
> I thought, that maybe using this function would also help when trying to add
> a month to a date. I would like to have
>
> "2006-01-31" + 1M = "2006-02-28"
>
> in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
> couldn't find any similar function in R for that. Am I missing some
> function?
>
> Best regards,
> Wojtek
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Diethelm Wuertz
Many thanks to Gabor for this comment.

For calendar tasks I have written a report (44 pages)  which may be
helpful for
many calendar manipulations using Rmetrics. Here is the link:

http://www.itp.phys.ethz.ch/econophysics/R/pdf/calendar.pdf

You can also access this "timeDate and timeSeries" white pages from the
Rmetrics home page: www.rmetrics.org

regards

Diethelm

Gabor Grothendieck wrote:

>Try this:
>
>library(fCalendar)
>dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
>dates <- timeLastDayInMonth(dates)
>
>
>On 2/4/06, Wojciech Slusarski <[hidden email]> wrote:
>  
>
>>Dear R users,
>>
>>Could anyone help me with creating a monthly series of dates?
>>When I try to do:
>>
>>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
>>
>>I get:
>>
>>    
>>
>>>dates
>>>      
>>>
>>[1] "Europe/Warsaw"
>> [1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
>> [6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
>>[11] [2006-10-31] [2006-12-01] [2006-12-31]
>>
>>Unfourtunately, I would like to heave end of month at each point.
>>I thought, that maybe using this function would also help when trying to add
>>a month to a date. I would like to have
>>
>>"2006-01-31" + 1M = "2006-02-28"
>>
>>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
>>couldn't find any similar function in R for that. Am I missing some
>>function?
>>
>>Best regards,
>>Wojtek
>>
>>       [[alternative HTML version deleted]]
>>
>>_______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>
>>    
>>
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>  
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Diethelm Wuertz
In reply to this post by Gabor Grothendieck
Gabor Grothendieck wrote:

>Try this:
>
>library(fCalendar)
>dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
>dates <- timeLastDayInMonth(dates)
>  
>
Alternatively you can use

 > timeSequence(from = "2006-01-01", to = "2007-01-01", format =
+ "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
[1] "Europe/Warsaw"
 [1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
 [6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
[11] [2006-10-31] [2006-11-30] [2006-12-31]

Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !

DW

>
>On 2/4/06, Wojciech Slusarski <[hidden email]> wrote:
>  
>
>>Dear R users,
>>
>>Could anyone help me with creating a monthly series of dates?
>>When I try to do:
>>
>>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
>>
>>I get:
>>
>>    
>>
>>>dates
>>>      
>>>
>>[1] "Europe/Warsaw"
>> [1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
>> [6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
>>[11] [2006-10-31] [2006-12-01] [2006-12-31]
>>
>>Unfourtunately, I would like to heave end of month at each point.
>>I thought, that maybe using this function would also help when trying to add
>>a month to a date. I would like to have
>>
>>"2006-01-31" + 1M = "2006-02-28"
>>
>>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
>>couldn't find any similar function in R for that. Am I missing some
>>function?
>>
>>Best regards,
>>Wojtek
>>
>>       [[alternative HTML version deleted]]
>>
>>_______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>
>>    
>>
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>  
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Vivek Satsangi
In reply to this post by Wojciech Slusarski
1. Why does subtracting 3600*24 (seconds, I presume) in DW's solution work?

2. Does anyone who uses gmail and gets this  DL's emails in digest
form have a good method for responding to just one message (I use
reply/edit subject/delete everything from the quoted material except
for the message I want to respond to -- That's obviously not much fun
when trying to respond to an r-help message for which the digest can
be long, and where one can end up fat-fingering the cut&paste of the
message (thus breaking the thread. )


> Gabor Grothendieck wrote:
>
> >Try this:
> >
> >library(fCalendar)
> >dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
> >"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> >dates <- timeLastDayInMonth(dates)
> >
> >
> Alternatively you can use
>
>  > timeSequence(from = "2006-01-01", to = "2007-01-01", format =
> + "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
> [1] "Europe/Warsaw"
>  [1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
>  [6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
> [11] [2006-10-31] [2006-11-30] [2006-12-31]
>
> Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !
>
> DW
>
[Other quoted messages deleted]
--
-- Vivek Satsangi
Student, Rochester, NY USA

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Wojciech Slusarski
In reply to this post by Diethelm Wuertz
Well,

Thanks for the help, but I thought about something more general, that could
allow me adding n months to a date. I am sorry, that my question wasn't so
clear. If someone needed, here's the code:


EMONTH =
function(date.s, n)
{
  #function adds 'n' months to 'date.s'
  dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"), length.out =
n+1, by = "month")
  if(atoms(date.s)[1,"d"]>28)
  {
      if(atoms(dates)[n+1,"d"]<4)
   {
      emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
   } else {
     emonth = dates[n+1]
   }
  } else {
    emonth = dates[n+1]
  }
  emonth
}



# test

date.s = timeDate("2005-12-30")

> EMONTH(date.s, 2)@Data
[1] "2006-02-28"
Best regards,
Wojtek


2006/2/5, Diethelm Wuertz <[hidden email]>:

>
> Gabor Grothendieck wrote:
>
> >Try this:
> >
> >library(fCalendar)
> >dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
> >"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> >dates <- timeLastDayInMonth(dates)
> >
> >
> Alternatively you can use
>
> > timeSequence(from = "2006-01-01", to = "2007-01-01", format =
> + "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
> [1] "Europe/Warsaw"
> [1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
> [6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
> [11] [2006-10-31] [2006-11-30] [2006-12-31]
>
> Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !
>
> DW
>
> >
> >On 2/4/06, Wojciech Slusarski <[hidden email]> wrote:
> >
> >
> >>Dear R users,
> >>
> >>Could anyone help me with creating a monthly series of dates?
> >>When I try to do:
> >>
> >>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
> >>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> >>
> >>I get:
> >>
> >>
> >>
> >>>dates
> >>>
> >>>
> >>[1] "Europe/Warsaw"
> >> [1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
> >> [6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
> >>[11] [2006-10-31] [2006-12-01] [2006-12-31]
> >>
> >>Unfourtunately, I would like to heave end of month at each point.
> >>I thought, that maybe using this function would also help when trying to
> add
> >>a month to a date. I would like to have
> >>
> >>"2006-01-31" + 1M = "2006-02-28"
> >>
> >>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
> >>couldn't find any similar function in R for that. Am I missing some
> >>function?
> >>
> >>Best regards,
> >>Wojtek
> >>
> >>       [[alternative HTML version deleted]]
> >>
> >>_______________________________________________
> >>[hidden email] mailing list
> >>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >>
> >>
> >>
> >
> >_______________________________________________
> >[hidden email] mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >
> >
> >
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Wojciech Slusarski
I thought a bit about the function below, and see a problem, when n<0 (I
would like to decrease the date.s by one month). I still have no Idea how to
implement it.

Best regards,
Wojtek


2006/2/5, Wojciech Slusarski <[hidden email]>:

>
> Well,
>
> Thanks for the help, but I thought about something more general, that
> could allow me adding n months to a date. I am sorry, that my question
> wasn't so clear. If someone needed, here's the code:
>
>
> EMONTH =
> function(date.s, n)
> {
>   #function adds 'n' months to 'date.s'
>   dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"), length.out= n+1, by = "month")
>   if(atoms(date.s)[1,"d"]>28)
>   {
>       if(atoms(dates)[n+1,"d"]<4)
>    {
>       emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
>    } else {
>      emonth = dates[n+1]
>    }
>   } else {
>     emonth = dates[n+1]
>   }
>   emonth
> }
>
>
>
> # test
>
> date.s = timeDate("2005-12-30")
>
> > EMONTH(date.s, 2)@Data
> [1] "2006-02-28"
> Best regards,
> Wojtek
>
>
> 2006/2/5, Diethelm Wuertz <[hidden email]>:
> >
> > Gabor Grothendieck wrote:
> >
> > >Try this:
> > >
> > >library(fCalendar)
> > >dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
> > >"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > >dates <- timeLastDayInMonth(dates)
> > >
> > >
> > Alternatively you can use
> >
> > > timeSequence(from = "2006-01-01", to = "2007-01-01", format =
> > + "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
> > [1] "Europe/Warsaw"
> > [1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
> > [6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
> > [11] [2006-10-31] [2006-11-30] [2006-12-31]
> >
> > Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !
> >
> > DW
> >
> > >
> > >On 2/4/06, Wojciech Slusarski < [hidden email]> wrote:
> > >
> > >
> > >>Dear R users,
> > >>
> > >>Could anyone help me with creating a monthly series of dates?
> > >>When I try to do:
> > >>
> > >>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
> > >>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > >>
> > >>I get:
> > >>
> > >>
> > >>
> > >>>dates
> > >>>
> > >>>
> > >>[1] "Europe/Warsaw"
> > >> [1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
> > >> [6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
> > >>[11] [2006-10-31] [2006-12-01] [2006-12-31]
> > >>
> > >>Unfourtunately, I would like to heave end of month at each point.
> > >>I thought, that maybe using this function would also help when trying
> > to add
> > >>a month to a date. I would like to have
> > >>
> > >>"2006-01-31" + 1M = "2006-02-28"
> > >>
> > >>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
> > >>couldn't find any similar function in R for that. Am I missing some
> > >>function?
> > >>
> > >>Best regards,
> > >>Wojtek
> > >>
> > >>       [[alternative HTML version deleted]]
> > >>
> > >>_______________________________________________
> > >> [hidden email] mailing list
> > >>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > >>
> > >>
> > >>
> > >
> > >_______________________________________________
> > >[hidden email] mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > >
> > >
> > >
> >
> > _______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >
>
>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Gabor Grothendieck
>From your series of posts I assume you really only need the last day
of the month that is m months from now but the following could easily
be enhanced to work in general.  It uses the yearmon class from zoo.
It works with positive and negative m:

library(zoo)
m <- -10
date.t <- date.s
date.t@Data <- as.POSIXlt(as.Date(as.yearmon(as.POSIXct(date.s))+m/12, frac=1))


On 2/5/06, Wojciech Slusarski <[hidden email]> wrote:

> I thought a bit about the function below, and see a problem, when n<0 (I
> would like to decrease the date.s by one month). I still have no Idea how to
> implement it.
>
> Best regards,
> Wojtek
>
>
> 2006/2/5, Wojciech Slusarski <[hidden email]>:
> >
> > Well,
> >
> > Thanks for the help, but I thought about something more general, that
> > could allow me adding n months to a date. I am sorry, that my question
> > wasn't so clear. If someone needed, here's the code:
> >
> >
> > EMONTH =
> > function(date.s, n)
> > {
> >   #function adds 'n' months to 'date.s'
> >   dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"), length.out= n+1, by = "month")
> >   if(atoms(date.s)[1,"d"]>28)
> >   {
> >       if(atoms(dates)[n+1,"d"]<4)
> >    {
> >       emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
> >    } else {
> >      emonth = dates[n+1]
> >    }
> >   } else {
> >     emonth = dates[n+1]
> >   }
> >   emonth
> > }
> >
> >
> >
> > # test
> >
> > date.s = timeDate("2005-12-30")
> >
> > > EMONTH(date.s, 2)@Data
> > [1] "2006-02-28"
> > Best regards,
> > Wojtek
> >
> >
> > 2006/2/5, Diethelm Wuertz <[hidden email]>:
> > >
> > > Gabor Grothendieck wrote:
> > >
> > > >Try this:
> > > >
> > > >library(fCalendar)
> > > >dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
> > > >"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > > >dates <- timeLastDayInMonth(dates)
> > > >
> > > >
> > > Alternatively you can use
> > >
> > > > timeSequence(from = "2006-01-01", to = "2007-01-01", format =
> > > + "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
> > > [1] "Europe/Warsaw"
> > > [1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
> > > [6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
> > > [11] [2006-10-31] [2006-11-30] [2006-12-31]
> > >
> > > Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !
> > >
> > > DW
> > >
> > > >
> > > >On 2/4/06, Wojciech Slusarski < [hidden email]> wrote:
> > > >
> > > >
> > > >>Dear R users,
> > > >>
> > > >>Could anyone help me with creating a monthly series of dates?
> > > >>When I try to do:
> > > >>
> > > >>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
> > > >>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > > >>
> > > >>I get:
> > > >>
> > > >>
> > > >>
> > > >>>dates
> > > >>>
> > > >>>
> > > >>[1] "Europe/Warsaw"
> > > >> [1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
> > > >> [6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
> > > >>[11] [2006-10-31] [2006-12-01] [2006-12-31]
> > > >>
> > > >>Unfourtunately, I would like to heave end of month at each point.
> > > >>I thought, that maybe using this function would also help when trying
> > > to add
> > > >>a month to a date. I would like to have
> > > >>
> > > >>"2006-01-31" + 1M = "2006-02-28"
> > > >>
> > > >>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
> > > >>couldn't find any similar function in R for that. Am I missing some
> > > >>function?
> > > >>
> > > >>Best regards,
> > > >>Wojtek
> > > >>
> > > >>       [[alternative HTML version deleted]]
> > > >>
> > > >>_______________________________________________
> > > >> [hidden email] mailing list
> > > >>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > >>
> > > >>
> > > >>
> > > >
> > > >_______________________________________________
> > > >[hidden email] mailing list
> > > > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > >
> > > >
> > > >
> > >
> > > _______________________________________________
> > > [hidden email] mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > >
> >
> >
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Diethelm Wuertz
In reply to this post by Wojciech Slusarski

Here comes a function which computes  time and date for the last Day
n-month ahead.
It is Rmetrics conform and preserves time zone, daylight saving and
Financial Centers.

Is that what you wanted?

DW



timeLastDayNMonthAhead =
function(charvec, month, format = "%Y-%m-%d", FinCenter = "GMT")
{
    if (Sys.getenv("TZ")[[1]] != "GMT") warning("Set timezone to GMT!")
    if (FinCenter == "") FinCenter = "GMT"
    if (month < 0) stop("month must be positive")
    date = timeLastDayInMonth(charvec, format = format, FinCenter =
FinCenter)
    for (i in 1:month) date = timeLastDayInMonth(date, format = format,
        FinCenter = FinCenter) + 3600*24
       
    # Return Value:
    timeLastDayInMonth(date, format = format, FinCenter = FinCenter)
}

# Example:
 > timeLastDayNMonthAhead(c("2005-12-25", "2006-02-28"), month = 2)
[1] "GMT"
[1] [2006-02-28] [2006-04-30]





Wojciech Slusarski wrote:

>I thought a bit about the function below, and see a problem, when n<0 (I
>would like to decrease the date.s by one month). I still have no Idea how to
>implement it.
>
>Best regards,
>Wojtek
>
>
>2006/2/5, Wojciech Slusarski <[hidden email]>:
>  
>
>>Well,
>>
>>Thanks for the help, but I thought about something more general, that
>>could allow me adding n months to a date. I am sorry, that my question
>>wasn't so clear. If someone needed, here's the code:
>>
>>
>>EMONTH =
>>function(date.s, n)
>>{
>>  #function adds 'n' months to 'date.s'
>>  dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"), length.out= n+1, by = "month")
>>  if(atoms(date.s)[1,"d"]>28)
>>  {
>>      if(atoms(dates)[n+1,"d"]<4)
>>   {
>>      emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
>>   } else {
>>     emonth = dates[n+1]
>>   }
>>  } else {
>>    emonth = dates[n+1]
>>  }
>>  emonth
>>}
>>
>>
>>
>># test
>>
>>date.s = timeDate("2005-12-30")
>>
>>    
>>
>>>EMONTH(date.s, 2)@Data
>>>      
>>>
>>[1] "2006-02-28"
>>Best regards,
>>Wojtek
>>
>>
>>2006/2/5, Diethelm Wuertz <[hidden email]>:
>>    
>>
>>>Gabor Grothendieck wrote:
>>>
>>>      
>>>
>>>>Try this:
>>>>
>>>>library(fCalendar)
>>>>dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
>>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
>>>>dates <- timeLastDayInMonth(dates)
>>>>
>>>>
>>>>        
>>>>
>>>Alternatively you can use
>>>
>>>      
>>>
>>>>timeSequence(from = "2006-01-01", to = "2007-01-01", format =
>>>>        
>>>>
>>>+ "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
>>>[1] "Europe/Warsaw"
>>>[1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
>>>[6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
>>>[11] [2006-10-31] [2006-11-30] [2006-12-31]
>>>
>>>Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !
>>>
>>>DW
>>>
>>>      
>>>
>>>>On 2/4/06, Wojciech Slusarski < [hidden email]> wrote:
>>>>
>>>>
>>>>        
>>>>
>>>>>Dear R users,
>>>>>
>>>>>Could anyone help me with creating a monthly series of dates?
>>>>>When I try to do:
>>>>>
>>>>>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
>>>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
>>>>>
>>>>>I get:
>>>>>
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>>>dates
>>>>>>
>>>>>>
>>>>>>            
>>>>>>
>>>>>[1] "Europe/Warsaw"
>>>>>[1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
>>>>>[6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
>>>>>[11] [2006-10-31] [2006-12-01] [2006-12-31]
>>>>>
>>>>>Unfourtunately, I would like to heave end of month at each point.
>>>>>I thought, that maybe using this function would also help when trying
>>>>>          
>>>>>
>>>to add
>>>      
>>>
>>>>>a month to a date. I would like to have
>>>>>
>>>>>"2006-01-31" + 1M = "2006-02-28"
>>>>>
>>>>>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
>>>>>couldn't find any similar function in R for that. Am I missing some
>>>>>function?
>>>>>
>>>>>Best regards,
>>>>>Wojtek
>>>>>
>>>>>      [[alternative HTML version deleted]]
>>>>>
>>>>>_______________________________________________
>>>>>[hidden email] mailing list
>>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>>>
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>_______________________________________________
>>>>[hidden email] mailing list
>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>>
>>>>
>>>>
>>>>        
>>>>
>>>_______________________________________________
>>>[hidden email] mailing list
>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>
>>>      
>>>
>>    
>>
>
> [[alternative HTML version deleted]]
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>  
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Wojciech Slusarski
Well, thanks for the help, but not exactly. The function I would like to
have would give results for starting dates

charvec = c("2005-12-25", "2006-01-30")

>function_name(charvec,+1M)

[1] [2006-01-25] [2006-02-28]

So it's adding one month to a date, but when it exceeds the last day of the
next month, it gives the las day.

The other way could be also useful:

charvec = c("2005-12-25", "2006-02-28")

>function_name(charvec,-1M)

[1] [2005-11-25] [2006-01-31]
#first date was decreased by one month, the second was decreased to the last
day of january (not 28th January).

Having such functions, one could set up proper dates for tenors in discount
curve. In the function you published few days ago, you setup fixed year
fractions for each tenor, which don't take into account holidays etc.

Best regards,
Wojtek

2006/2/5, Diethelm Wuertz <[hidden email]>:

>
>
> Here comes a function which computes  time and date for the last Day
> n-month ahead.
> It is Rmetrics conform and preserves time zone, daylight saving and
> Financial Centers.
>
> Is that what you wanted?
>
> DW
>
>
>
> timeLastDayNMonthAhead =
> function(charvec, month, format = "%Y-%m-%d", FinCenter = "GMT")
> {
>    if (Sys.getenv("TZ")[[1]] != "GMT") warning("Set timezone to GMT!")
>    if (FinCenter == "") FinCenter = "GMT"
>    if (month < 0) stop("month must be positive")
>    date = timeLastDayInMonth(charvec, format = format, FinCenter =
> FinCenter)
>    for (i in 1:month) date = timeLastDayInMonth(date, format = format,
>        FinCenter = FinCenter) + 3600*24
>
>    # Return Value:
>    timeLastDayInMonth(date, format = format, FinCenter = FinCenter)
> }
>
> # Example:
> > timeLastDayNMonthAhead(c("2005-12-25", "2006-02-28"), month = 2)
> [1] "GMT"
> [1] [2006-02-28] [2006-04-30]
>
>
>
>
>
> Wojciech Slusarski wrote:
>
> >I thought a bit about the function below, and see a problem, when n<0 (I
> >would like to decrease the date.s by one month). I still have no Idea how
> to
> >implement it.
> >
> >Best regards,
> >Wojtek
> >
> >
> >2006/2/5, Wojciech Slusarski <[hidden email]>:
> >
> >
> >>Well,
> >>
> >>Thanks for the help, but I thought about something more general, that
> >>could allow me adding n months to a date. I am sorry, that my question
> >>wasn't so clear. If someone needed, here's the code:
> >>
> >>
> >>EMONTH =
> >>function(date.s, n)
> >>{
> >>  #function adds 'n' months to 'date.s'
> >>  dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"),
> length.out= n+1, by = "month")
> >>  if(atoms(date.s)[1,"d"]>28)
> >>  {
> >>      if(atoms(dates)[n+1,"d"]<4)
> >>   {
> >>      emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
> >>   } else {
> >>     emonth = dates[n+1]
> >>   }
> >>  } else {
> >>    emonth = dates[n+1]
> >>  }
> >>  emonth
> >>}
> >>
> >>
> >>
> >># test
> >>
> >>date.s = timeDate("2005-12-30")
> >>
> >>
> >>
> >>>EMONTH(date.s, 2)@Data
> >>>
> >>>
> >>[1] "2006-02-28"
> >>Best regards,
> >>Wojtek
> >>
> >>
> >>2006/2/5, Diethelm Wuertz <[hidden email]>:
> >>
> >>
> >>>Gabor Grothendieck wrote:
> >>>
> >>>
> >>>
> >>>>Try this:
> >>>>
> >>>>library(fCalendar)
> >>>>dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
> >>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> >>>>dates <- timeLastDayInMonth(dates)
> >>>>
> >>>>
> >>>>
> >>>>
> >>>Alternatively you can use
> >>>
> >>>
> >>>
> >>>>timeSequence(from = "2006-01-01", to = "2007-01-01", format =
> >>>>
> >>>>
> >>>+ "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
> >>>[1] "Europe/Warsaw"
> >>>[1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
> >>>[6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
> >>>[11] [2006-10-31] [2006-11-30] [2006-12-31]
> >>>
> >>>Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !
> >>>
> >>>DW
> >>>
> >>>
> >>>
> >>>>On 2/4/06, Wojciech Slusarski < [hidden email]> wrote:
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>>Dear R users,
> >>>>>
> >>>>>Could anyone help me with creating a monthly series of dates?
> >>>>>When I try to do:
> >>>>>
> >>>>>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
> >>>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> >>>>>
> >>>>>I get:
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>>dates
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>[1] "Europe/Warsaw"
> >>>>>[1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
> >>>>>[6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
> >>>>>[11] [2006-10-31] [2006-12-01] [2006-12-31]
> >>>>>
> >>>>>Unfourtunately, I would like to heave end of month at each point.
> >>>>>I thought, that maybe using this function would also help when trying
> >>>>>
> >>>>>
> >>>to add
> >>>
> >>>
> >>>>>a month to a date. I would like to have
> >>>>>
> >>>>>"2006-01-31" + 1M = "2006-02-28"
> >>>>>
> >>>>>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
> >>>>>couldn't find any similar function in R for that. Am I missing some
> >>>>>function?
> >>>>>
> >>>>>Best regards,
> >>>>>Wojtek
> >>>>>
> >>>>>      [[alternative HTML version deleted]]
> >>>>>
> >>>>>_______________________________________________
> >>>>>[hidden email] mailing list
> >>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>_______________________________________________
> >>>>[hidden email] mailing list
> >>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>_______________________________________________
> >>>[hidden email] mailing list
> >>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >>>
> >>>
> >>>
> >>
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> >_______________________________________________
> >[hidden email] mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >
> >
> >
>
>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Parlamis Franklin
you want end.end convention (like for the interbank deposits underlying BBA Libor quotations).  here's some code I wrote recently.  caveats:

(i)  i just learned to code, so this probably doesn't exhibit professional programming standards
(ii) you need the Methods package (which is default loaded when R opens)
(iii) the functions are not yet complete (in that i don't deal with improper arguments as fully as i intend to)
(iv) you will need to separately check whether the output dates are business days, and then apply the mod.following convention or whichever other convention you want; all these functions do is move you forward an increment subject to the end.end standard.
(v) the date arguments are not character vectors as you envision; rather they are objects that can be compelled to POSIXlt dates via as.POSIXlt, or timeDate objects (you could easily add a method to convert the character vectors to POSIXlt objects or timeDate objects)
(vi) i'm pretty sure these functions won't work for negative increments as you request, but I thank you for pointing out that functionality, as it shouldn't be hard for me to alter the functions to get there.

----

########################
## last.of      
########################

last.of <- function(date) {
  date<-as.POSIXlt(date)
  switch(date[["mon"]]+1,31,28+leap.year(date[["year"]]+1900),31,30,31,30,31,31,30,31,30,31)}

setMethod("last.of", signature(date="timeDate"), function(date)
  last.of(date@Data))

is.last.of <- function(date) {
  date<-as.POSIXlt(date)
  date[["mday"]]==last.of(date)}

setMethod("is.last.of", signature(date="timeDate"), function(date)
  is.last.of(date@Data))

########################
## end.end      
########################

end.end <- function(date, num, inc) {
  date<-as.POSIXlt(date)
  trial<-as.POSIXlt(seq(from=date,len=2,by=paste(num,inc))[2])
  if(inc=="month") {
    while((trial[["mon"]]-date[["mon"]])%%12>num)
      trial<-as.POSIXlt(seq(from=trial,len=2,by="-1 day")[2])
    if(is.last.of(date)) while(is.last.of(trial)==FALSE)
      trial<-as.POSIXlt(seq(from=trial,len=2,by="1 day")[2])}
  if(inc=="year") {
    while(trial[["mon"]]!=date[["mon"]])
      trial<-as.POSIXlt(seq(from=trial,len=2,by="-1 day")[2])
    if(is.last.of(date)) while(is.last.of(trial)==FALSE)
      trial<-as.POSIXlt(seq(from=trial,len=2,by="1 day")[2])}
  trial}

setMethod("end.end", signature(date="timeDate"), function(date, num, inc)
  end.end(date@Data, num, inc))

-----

franklin parlamis
 
On Sunday, February 05, 2006, at 12:18PM, Wojciech Slusarski <[hidden email]> wrote:

>Well, thanks for the help, but not exactly. The function I would like to
>have would give results for starting dates
>
>charvec = c("2005-12-25", "2006-01-30")
>
>>function_name(charvec,+1M)
>
>[1] [2006-01-25] [2006-02-28]
>
>So it's adding one month to a date, but when it exceeds the last day of the
>next month, it gives the las day.
>
>The other way could be also useful:
>
>charvec = c("2005-12-25", "2006-02-28")
>
>>function_name(charvec,-1M)
>
>[1] [2005-11-25] [2006-01-31]
>#first date was decreased by one month, the second was decreased to the last
>day of january (not 28th January).
>
>Having such functions, one could set up proper dates for tenors in discount
>curve. In the function you published few days ago, you setup fixed year
>fractions for each tenor, which don't take into account holidays etc.
>
>Best regards,
>Wojtek
>
>2006/2/5, Diethelm Wuertz <[hidden email]>:
>>
>>
>> Here comes a function which computes  time and date for the last Day
>> n-month ahead.
>> It is Rmetrics conform and preserves time zone, daylight saving and
>> Financial Centers.
>>
>> Is that what you wanted?
>>
>> DW
>>
>>
>>
>> timeLastDayNMonthAhead =
>> function(charvec, month, format = "%Y-%m-%d", FinCenter = "GMT")
>> {
>>    if (Sys.getenv("TZ")[[1]] != "GMT") warning("Set timezone to GMT!")
>>    if (FinCenter == "") FinCenter = "GMT"
>>    if (month < 0) stop("month must be positive")
>>    date = timeLastDayInMonth(charvec, format = format, FinCenter =
>> FinCenter)
>>    for (i in 1:month) date = timeLastDayInMonth(date, format = format,
>>        FinCenter = FinCenter) + 3600*24
>>
>>    # Return Value:
>>    timeLastDayInMonth(date, format = format, FinCenter = FinCenter)
>> }
>>
>> # Example:
>> > timeLastDayNMonthAhead(c("2005-12-25", "2006-02-28"), month = 2)
>> [1] "GMT"
>> [1] [2006-02-28] [2006-04-30]
>>
>>
>>
>>
>>
>> Wojciech Slusarski wrote:
>>
>> >I thought a bit about the function below, and see a problem, when n<0 (I
>> >would like to decrease the date.s by one month). I still have no Idea how
>> to
>> >implement it.
>> >
>> >Best regards,
>> >Wojtek
>> >
>> >
>> >2006/2/5, Wojciech Slusarski <[hidden email]>:
>> >
>> >
>> >>Well,
>> >>
>> >>Thanks for the help, but I thought about something more general, that
>> >>could allow me adding n months to a date. I am sorry, that my question
>> >>wasn't so clear. If someone needed, here's the code:
>> >>
>> >>
>> >>EMONTH =
>> >>function(date.s, n)
>> >>{
>> >>  #function adds 'n' months to 'date.s'
>> >>  dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"),
>> length.out= n+1, by = "month")
>> >>  if(atoms(date.s)[1,"d"]>28)
>> >>  {
>> >>      if(atoms(dates)[n+1,"d"]<4)
>> >>   {
>> >>      emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
>> >>   } else {
>> >>     emonth = dates[n+1]
>> >>   }
>> >>  } else {
>> >>    emonth = dates[n+1]
>> >>  }
>> >>  emonth
>> >>}
>> >>
>> >>
>> >>
>> >># test
>> >>
>> >>date.s = timeDate("2005-12-30")
>> >>
>> >>
>> >>
>> >>>EMONTH(date.s, 2)@Data
>> >>>
>> >>>
>> >>[1] "2006-02-28"
>> >>Best regards,
>> >>Wojtek
>> >>
>> >>
>> >>2006/2/5, Diethelm Wuertz <[hidden email]>:
>> >>
>> >>
>> >>>Gabor Grothendieck wrote:
>> >>>
>> >>>
>> >>>
>> >>>>Try this:
>> >>>>
>> >>>>library(fCalendar)
>> >>>>dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
>> >>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
>> >>>>dates <- timeLastDayInMonth(dates)
>> >>>>
>> >>>>
>> >>>>
>> >>>>
>> >>>Alternatively you can use
>> >>>
>> >>>
>> >>>
>> >>>>timeSequence(from = "2006-01-01", to = "2007-01-01", format =
>> >>>>
>> >>>>
>> >>>+ "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
>> >>>[1] "Europe/Warsaw"
>> >>>[1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
>> >>>[6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
>> >>>[11] [2006-10-31] [2006-11-30] [2006-12-31]
>> >>>
>> >>>Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !
>> >>>
>> >>>DW
>> >>>
>> >>>
>> >>>
>> >>>>On 2/4/06, Wojciech Slusarski < [hidden email]> wrote:
>> >>>>
>> >>>>
>> >>>>
>> >>>>
>> >>>>>Dear R users,
>> >>>>>
>> >>>>>Could anyone help me with creating a monthly series of dates?
>> >>>>>When I try to do:
>> >>>>>
>> >>>>>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
>> >>>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
>> >>>>>
>> >>>>>I get:
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>>>dates
>> >>>>>>
>> >>>>>>
>> >>>>>>
>> >>>>>>
>> >>>>>[1] "Europe/Warsaw"
>> >>>>>[1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
>> >>>>>[6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
>> >>>>>[11] [2006-10-31] [2006-12-01] [2006-12-31]
>> >>>>>
>> >>>>>Unfourtunately, I would like to heave end of month at each point.
>> >>>>>I thought, that maybe using this function would also help when trying
>> >>>>>
>> >>>>>
>> >>>to add
>> >>>
>> >>>
>> >>>>>a month to a date. I would like to have
>> >>>>>
>> >>>>>"2006-01-31" + 1M = "2006-02-28"
>> >>>>>
>> >>>>>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
>> >>>>>couldn't find any similar function in R for that. Am I missing some
>> >>>>>function?
>> >>>>>
>> >>>>>Best regards,
>> >>>>>Wojtek
>> >>>>>
>> >>>>>      [[alternative HTML version deleted]]
>> >>>>>
>> >>>>>_______________________________________________
>> >>>>>[hidden email] mailing list
>> >>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>>
>> >>>>_______________________________________________
>> >>>>[hidden email] mailing list
>> >>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> >>>>
>> >>>>
>> >>>>
>> >>>>
>> >>>>
>> >>>_______________________________________________
>> >>>[hidden email] mailing list
>> >>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> >>>
>> >>>
>> >>>
>> >>
>> >>
>> >
>> >       [[alternative HTML version deleted]]
>> >
>> >_______________________________________________
>> >[hidden email] mailing list
>> >https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> >
>> >
>> >
>>
>>
>
> [[alternative HTML version deleted]]
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Gabor Grothendieck
In reply to this post by Wojciech Slusarski
Try this.  It takes a vector of dates in any class that has
an as.Date method and returns a vector of Date class whose
add m months to each element where m may be negative.
Internally it makes use of the yearmon class and methods
in the zoo library; however, as shown in the examples
its simple to convert to class timeDate.  The way it
works is to calculate the date m months hence at the
beginning of the month + day - 1 and the end of the month
and it takes whichever is smaller.

library(zoo)
addmon <- function(x = Sys.Date(), m = 1) {
        dd <- as.Date(x)
        ym <- as.yearmon(dd)
        pmin(as.Date(ym + m/12) + as.numeric(format(dd, "%d")) - 1,
                as.Date(ym + m/12, frac = 1))
}

# test
charvec <- c("2005-12-25", "2006-01-30")
addmon(charvec)
addmon(charvec, -11)

library(fCalendar)
timeDate(format(addmon(charvec)))


On 2/5/06, Wojciech Slusarski <[hidden email]> wrote:

> Well, thanks for the help, but not exactly. The function I would like to
> have would give results for starting dates
>
> charvec = c("2005-12-25", "2006-01-30")
>
> >function_name(charvec,+1M)
>
> [1] [2006-01-25] [2006-02-28]
>
> So it's adding one month to a date, but when it exceeds the last day of the
> next month, it gives the las day.
>
> The other way could be also useful:
>
> charvec = c("2005-12-25", "2006-02-28")
>
> >function_name(charvec,-1M)
>
> [1] [2005-11-25] [2006-01-31]
> #first date was decreased by one month, the second was decreased to the last
> day of january (not 28th January).
>
> Having such functions, one could set up proper dates for tenors in discount
> curve. In the function you published few days ago, you setup fixed year
> fractions for each tenor, which don't take into account holidays etc.
>
> Best regards,
> Wojtek
>
> 2006/2/5, Diethelm Wuertz <[hidden email]>:
> >
> >
> > Here comes a function which computes  time and date for the last Day
> > n-month ahead.
> > It is Rmetrics conform and preserves time zone, daylight saving and
> > Financial Centers.
> >
> > Is that what you wanted?
> >
> > DW
> >
> >
> >
> > timeLastDayNMonthAhead =
> > function(charvec, month, format = "%Y-%m-%d", FinCenter = "GMT")
> > {
> >    if (Sys.getenv("TZ")[[1]] != "GMT") warning("Set timezone to GMT!")
> >    if (FinCenter == "") FinCenter = "GMT"
> >    if (month < 0) stop("month must be positive")
> >    date = timeLastDayInMonth(charvec, format = format, FinCenter =
> > FinCenter)
> >    for (i in 1:month) date = timeLastDayInMonth(date, format = format,
> >        FinCenter = FinCenter) + 3600*24
> >
> >    # Return Value:
> >    timeLastDayInMonth(date, format = format, FinCenter = FinCenter)
> > }
> >
> > # Example:
> > > timeLastDayNMonthAhead(c("2005-12-25", "2006-02-28"), month = 2)
> > [1] "GMT"
> > [1] [2006-02-28] [2006-04-30]
> >
> >
> >
> >
> >
> > Wojciech Slusarski wrote:
> >
> > >I thought a bit about the function below, and see a problem, when n<0 (I
> > >would like to decrease the date.s by one month). I still have no Idea how
> > to
> > >implement it.
> > >
> > >Best regards,
> > >Wojtek
> > >
> > >
> > >2006/2/5, Wojciech Slusarski <[hidden email]>:
> > >
> > >
> > >>Well,
> > >>
> > >>Thanks for the help, but I thought about something more general, that
> > >>could allow me adding n months to a date. I am sorry, that my question
> > >>wasn't so clear. If someone needed, here's the code:
> > >>
> > >>
> > >>EMONTH =
> > >>function(date.s, n)
> > >>{
> > >>  #function adds 'n' months to 'date.s'
> > >>  dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"),
> > length.out= n+1, by = "month")
> > >>  if(atoms(date.s)[1,"d"]>28)
> > >>  {
> > >>      if(atoms(dates)[n+1,"d"]<4)
> > >>   {
> > >>      emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
> > >>   } else {
> > >>     emonth = dates[n+1]
> > >>   }
> > >>  } else {
> > >>    emonth = dates[n+1]
> > >>  }
> > >>  emonth
> > >>}
> > >>
> > >>
> > >>
> > >># test
> > >>
> > >>date.s = timeDate("2005-12-30")
> > >>
> > >>
> > >>
> > >>>EMONTH(date.s, 2)@Data
> > >>>
> > >>>
> > >>[1] "2006-02-28"
> > >>Best regards,
> > >>Wojtek
> > >>
> > >>
> > >>2006/2/5, Diethelm Wuertz <[hidden email]>:
> > >>
> > >>
> > >>>Gabor Grothendieck wrote:
> > >>>
> > >>>
> > >>>
> > >>>>Try this:
> > >>>>
> > >>>>library(fCalendar)
> > >>>>dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
> > >>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > >>>>dates <- timeLastDayInMonth(dates)
> > >>>>
> > >>>>
> > >>>>
> > >>>>
> > >>>Alternatively you can use
> > >>>
> > >>>
> > >>>
> > >>>>timeSequence(from = "2006-01-01", to = "2007-01-01", format =
> > >>>>
> > >>>>
> > >>>+ "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
> > >>>[1] "Europe/Warsaw"
> > >>>[1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
> > >>>[6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
> > >>>[11] [2006-10-31] [2006-11-30] [2006-12-31]
> > >>>
> > >>>Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !
> > >>>
> > >>>DW
> > >>>
> > >>>
> > >>>
> > >>>>On 2/4/06, Wojciech Slusarski < [hidden email]> wrote:
> > >>>>
> > >>>>
> > >>>>
> > >>>>
> > >>>>>Dear R users,
> > >>>>>
> > >>>>>Could anyone help me with creating a monthly series of dates?
> > >>>>>When I try to do:
> > >>>>>
> > >>>>>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
> > >>>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > >>>>>
> > >>>>>I get:
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>>dates
> > >>>>>>
> > >>>>>>
> > >>>>>>
> > >>>>>>
> > >>>>>[1] "Europe/Warsaw"
> > >>>>>[1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
> > >>>>>[6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
> > >>>>>[11] [2006-10-31] [2006-12-01] [2006-12-31]
> > >>>>>
> > >>>>>Unfourtunately, I would like to heave end of month at each point.
> > >>>>>I thought, that maybe using this function would also help when trying
> > >>>>>
> > >>>>>
> > >>>to add
> > >>>
> > >>>
> > >>>>>a month to a date. I would like to have
> > >>>>>
> > >>>>>"2006-01-31" + 1M = "2006-02-28"
> > >>>>>
> > >>>>>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
> > >>>>>couldn't find any similar function in R for that. Am I missing some
> > >>>>>function?
> > >>>>>
> > >>>>>Best regards,
> > >>>>>Wojtek
> > >>>>>
> > >>>>>      [[alternative HTML version deleted]]
> > >>>>>
> > >>>>>_______________________________________________
> > >>>>>[hidden email] mailing list
> > >>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>_______________________________________________
> > >>>>[hidden email] mailing list
> > >>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > >>>>
> > >>>>
> > >>>>
> > >>>>
> > >>>>
> > >>>_______________________________________________
> > >>>[hidden email] mailing list
> > >>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > >>>
> > >>>
> > >>>
> > >>
> > >>
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > >_______________________________________________
> > >[hidden email] mailing list
> > >https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > >
> > >
> > >
> >
> >
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Gabor Grothendieck
I have made some minor tweaks to shorten the function:

library(zoo)
addmon <- function(x = Sys.Date(), m = 1) {
       dd <- as.Date(x)
       ym <- as.yearmon(dd) + m/12
       pmin(as.Date(ym) + as.POSIXlt(dd)$mday - 1, as.Date(ym, 1))
}

# test
charvec <- c("2005-12-25", "2006-01-30")
addmon(charvec)
addmon(charvec, -11)

library(fCalendar)
timeDate(format(addmon(charvec)))


On 2/5/06, Gabor Grothendieck <[hidden email]> wrote:

> Try this.  It takes a vector of dates in any class that has
> an as.Date method and returns a vector of Date class whose
> add m months to each element where m may be negative.
> Internally it makes use of the yearmon class and methods
> in the zoo library; however, as shown in the examples
> its simple to convert to class timeDate.  The way it
> works is to calculate the date m months hence at the
> beginning of the month + day - 1 and the end of the month
> and it takes whichever is smaller.
>
> library(zoo)
> addmon <- function(x = Sys.Date(), m = 1) {
>        dd <- as.Date(x)
>        ym <- as.yearmon(dd)
>        pmin(as.Date(ym + m/12) + as.numeric(format(dd, "%d")) - 1,
>                as.Date(ym + m/12, frac = 1))
> }
>
> # test
> charvec <- c("2005-12-25", "2006-01-30")
> addmon(charvec)
> addmon(charvec, -11)
>
> library(fCalendar)
> timeDate(format(addmon(charvec)))
>
>
> On 2/5/06, Wojciech Slusarski <[hidden email]> wrote:
> > Well, thanks for the help, but not exactly. The function I would like to
> > have would give results for starting dates
> >
> > charvec = c("2005-12-25", "2006-01-30")
> >
> > >function_name(charvec,+1M)
> >
> > [1] [2006-01-25] [2006-02-28]
> >
> > So it's adding one month to a date, but when it exceeds the last day of the
> > next month, it gives the las day.
> >
> > The other way could be also useful:
> >
> > charvec = c("2005-12-25", "2006-02-28")
> >
> > >function_name(charvec,-1M)
> >
> > [1] [2005-11-25] [2006-01-31]
> > #first date was decreased by one month, the second was decreased to the last
> > day of january (not 28th January).
> >
> > Having such functions, one could set up proper dates for tenors in discount
> > curve. In the function you published few days ago, you setup fixed year
> > fractions for each tenor, which don't take into account holidays etc.
> >
> > Best regards,
> > Wojtek
> >
> > 2006/2/5, Diethelm Wuertz <[hidden email]>:
> > >
> > >
> > > Here comes a function which computes  time and date for the last Day
> > > n-month ahead.
> > > It is Rmetrics conform and preserves time zone, daylight saving and
> > > Financial Centers.
> > >
> > > Is that what you wanted?
> > >
> > > DW
> > >
> > >
> > >
> > > timeLastDayNMonthAhead =
> > > function(charvec, month, format = "%Y-%m-%d", FinCenter = "GMT")
> > > {
> > >    if (Sys.getenv("TZ")[[1]] != "GMT") warning("Set timezone to GMT!")
> > >    if (FinCenter == "") FinCenter = "GMT"
> > >    if (month < 0) stop("month must be positive")
> > >    date = timeLastDayInMonth(charvec, format = format, FinCenter =
> > > FinCenter)
> > >    for (i in 1:month) date = timeLastDayInMonth(date, format = format,
> > >        FinCenter = FinCenter) + 3600*24
> > >
> > >    # Return Value:
> > >    timeLastDayInMonth(date, format = format, FinCenter = FinCenter)
> > > }
> > >
> > > # Example:
> > > > timeLastDayNMonthAhead(c("2005-12-25", "2006-02-28"), month = 2)
> > > [1] "GMT"
> > > [1] [2006-02-28] [2006-04-30]
> > >
> > >
> > >
> > >
> > >
> > > Wojciech Slusarski wrote:
> > >
> > > >I thought a bit about the function below, and see a problem, when n<0 (I
> > > >would like to decrease the date.s by one month). I still have no Idea how
> > > to
> > > >implement it.
> > > >
> > > >Best regards,
> > > >Wojtek
> > > >
> > > >
> > > >2006/2/5, Wojciech Slusarski <[hidden email]>:
> > > >
> > > >
> > > >>Well,
> > > >>
> > > >>Thanks for the help, but I thought about something more general, that
> > > >>could allow me adding n months to a date. I am sorry, that my question
> > > >>wasn't so clear. If someone needed, here's the code:
> > > >>
> > > >>
> > > >>EMONTH =
> > > >>function(date.s, n)
> > > >>{
> > > >>  #function adds 'n' months to 'date.s'
> > > >>  dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"),
> > > length.out= n+1, by = "month")
> > > >>  if(atoms(date.s)[1,"d"]>28)
> > > >>  {
> > > >>      if(atoms(dates)[n+1,"d"]<4)
> > > >>   {
> > > >>      emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
> > > >>   } else {
> > > >>     emonth = dates[n+1]
> > > >>   }
> > > >>  } else {
> > > >>    emonth = dates[n+1]
> > > >>  }
> > > >>  emonth
> > > >>}
> > > >>
> > > >>
> > > >>
> > > >># test
> > > >>
> > > >>date.s = timeDate("2005-12-30")
> > > >>
> > > >>
> > > >>
> > > >>>EMONTH(date.s, 2)@Data
> > > >>>
> > > >>>
> > > >>[1] "2006-02-28"
> > > >>Best regards,
> > > >>Wojtek
> > > >>
> > > >>
> > > >>2006/2/5, Diethelm Wuertz <[hidden email]>:
> > > >>
> > > >>
> > > >>>Gabor Grothendieck wrote:
> > > >>>
> > > >>>
> > > >>>
> > > >>>>Try this:
> > > >>>>
> > > >>>>library(fCalendar)
> > > >>>>dates <- timeSequence(from = "2005-12-1", to = "2006-12-1", format =
> > > >>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > > >>>>dates <- timeLastDayInMonth(dates)
> > > >>>>
> > > >>>>
> > > >>>>
> > > >>>>
> > > >>>Alternatively you can use
> > > >>>
> > > >>>
> > > >>>
> > > >>>>timeSequence(from = "2006-01-01", to = "2007-01-01", format =
> > > >>>>
> > > >>>>
> > > >>>+ "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
> > > >>>[1] "Europe/Warsaw"
> > > >>>[1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31] [2006-04-30]
> > > >>>[6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31] [2006-09-30]
> > > >>>[11] [2006-10-31] [2006-11-30] [2006-12-31]
> > > >>>
> > > >>>Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5 !
> > > >>>
> > > >>>DW
> > > >>>
> > > >>>
> > > >>>
> > > >>>>On 2/4/06, Wojciech Slusarski < [hidden email]> wrote:
> > > >>>>
> > > >>>>
> > > >>>>
> > > >>>>
> > > >>>>>Dear R users,
> > > >>>>>
> > > >>>>>Could anyone help me with creating a monthly series of dates?
> > > >>>>>When I try to do:
> > > >>>>>
> > > >>>>>dates = timeSequence(from = "2005-12-31", to = "2006-12-31", format =
> > > >>>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > > >>>>>
> > > >>>>>I get:
> > > >>>>>
> > > >>>>>
> > > >>>>>
> > > >>>>>
> > > >>>>>
> > > >>>>>>dates
> > > >>>>>>
> > > >>>>>>
> > > >>>>>>
> > > >>>>>>
> > > >>>>>[1] "Europe/Warsaw"
> > > >>>>>[1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31] [2006-05-01]
> > > >>>>>[6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31] [2006-10-01]
> > > >>>>>[11] [2006-10-31] [2006-12-01] [2006-12-31]
> > > >>>>>
> > > >>>>>Unfourtunately, I would like to heave end of month at each point.
> > > >>>>>I thought, that maybe using this function would also help when trying
> > > >>>>>
> > > >>>>>
> > > >>>to add
> > > >>>
> > > >>>
> > > >>>>>a month to a date. I would like to have
> > > >>>>>
> > > >>>>>"2006-01-31" + 1M = "2006-02-28"
> > > >>>>>
> > > >>>>>in MS EXCEL I would use EDATE("2006-01-31";1), and that would work. I
> > > >>>>>couldn't find any similar function in R for that. Am I missing some
> > > >>>>>function?
> > > >>>>>
> > > >>>>>Best regards,
> > > >>>>>Wojtek
> > > >>>>>
> > > >>>>>      [[alternative HTML version deleted]]
> > > >>>>>
> > > >>>>>_______________________________________________
> > > >>>>>[hidden email] mailing list
> > > >>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > >>>>>
> > > >>>>>
> > > >>>>>
> > > >>>>>
> > > >>>>>
> > > >>>>_______________________________________________
> > > >>>>[hidden email] mailing list
> > > >>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > >>>>
> > > >>>>
> > > >>>>
> > > >>>>
> > > >>>>
> > > >>>_______________________________________________
> > > >>>[hidden email] mailing list
> > > >>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > >>>
> > > >>>
> > > >>>
> > > >>
> > > >>
> > > >
> > > >       [[alternative HTML version deleted]]
> > > >
> > > >_______________________________________________
> > > >[hidden email] mailing list
> > > >https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > >
> > > >
> > > >
> > >
> > >
> >
> >        [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

Wojciech Slusarski
Thanks a lot, that works exactly the way I needed.

Best regards,
Wojtek


2006/2/6, Gabor Grothendieck <[hidden email]>:

>
> I have made some minor tweaks to shorten the function:
>
> library(zoo)
> addmon <- function(x = Sys.Date(), m = 1) {
>       dd <- as.Date(x)
>       ym <- as.yearmon(dd) + m/12
>       pmin(as.Date(ym) + as.POSIXlt(dd)$mday - 1, as.Date(ym, 1))
> }
>
> # test
> charvec <- c("2005-12-25", "2006-01-30")
> addmon(charvec)
> addmon(charvec, -11)
>
> library(fCalendar)
> timeDate(format(addmon(charvec)))
>
>
> On 2/5/06, Gabor Grothendieck <[hidden email]> wrote:
> > Try this.  It takes a vector of dates in any class that has
> > an as.Date method and returns a vector of Date class whose
> > add m months to each element where m may be negative.
> > Internally it makes use of the yearmon class and methods
> > in the zoo library; however, as shown in the examples
> > its simple to convert to class timeDate.  The way it
> > works is to calculate the date m months hence at the
> > beginning of the month + day - 1 and the end of the month
> > and it takes whichever is smaller.
> >
> > library(zoo)
> > addmon <- function(x = Sys.Date(), m = 1) {
> >        dd <- as.Date(x)
> >        ym <- as.yearmon(dd)
> >        pmin(as.Date(ym + m/12) + as.numeric(format(dd, "%d")) - 1,
> >                as.Date(ym + m/12, frac = 1))
> > }
> >
> > # test
> > charvec <- c("2005-12-25", "2006-01-30")
> > addmon(charvec)
> > addmon(charvec, -11)
> >
> > library(fCalendar)
> > timeDate(format(addmon(charvec)))
> >
> >
> > On 2/5/06, Wojciech Slusarski <[hidden email]> wrote:
> > > Well, thanks for the help, but not exactly. The function I would like
> to
> > > have would give results for starting dates
> > >
> > > charvec = c("2005-12-25", "2006-01-30")
> > >
> > > >function_name(charvec,+1M)
> > >
> > > [1] [2006-01-25] [2006-02-28]
> > >
> > > So it's adding one month to a date, but when it exceeds the last day
> of the
> > > next month, it gives the las day.
> > >
> > > The other way could be also useful:
> > >
> > > charvec = c("2005-12-25", "2006-02-28")
> > >
> > > >function_name(charvec,-1M)
> > >
> > > [1] [2005-11-25] [2006-01-31]
> > > #first date was decreased by one month, the second was decreased to
> the last
> > > day of january (not 28th January).
> > >
> > > Having such functions, one could set up proper dates for tenors in
> discount
> > > curve. In the function you published few days ago, you setup fixed
> year
> > > fractions for each tenor, which don't take into account holidays etc.
> > >
> > > Best regards,
> > > Wojtek
> > >
> > > 2006/2/5, Diethelm Wuertz <[hidden email]>:
> > > >
> > > >
> > > > Here comes a function which computes  time and date for the last Day
> > > > n-month ahead.
> > > > It is Rmetrics conform and preserves time zone, daylight saving and
> > > > Financial Centers.
> > > >
> > > > Is that what you wanted?
> > > >
> > > > DW
> > > >
> > > >
> > > >
> > > > timeLastDayNMonthAhead =
> > > > function(charvec, month, format = "%Y-%m-%d", FinCenter = "GMT")
> > > > {
> > > >    if (Sys.getenv("TZ")[[1]] != "GMT") warning("Set timezone to
> GMT!")
> > > >    if (FinCenter == "") FinCenter = "GMT"
> > > >    if (month < 0) stop("month must be positive")
> > > >    date = timeLastDayInMonth(charvec, format = format, FinCenter =
> > > > FinCenter)
> > > >    for (i in 1:month) date = timeLastDayInMonth(date, format =
> format,
> > > >        FinCenter = FinCenter) + 3600*24
> > > >
> > > >    # Return Value:
> > > >    timeLastDayInMonth(date, format = format, FinCenter = FinCenter)
> > > > }
> > > >
> > > > # Example:
> > > > > timeLastDayNMonthAhead(c("2005-12-25", "2006-02-28"), month = 2)
> > > > [1] "GMT"
> > > > [1] [2006-02-28] [2006-04-30]
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > Wojciech Slusarski wrote:
> > > >
> > > > >I thought a bit about the function below, and see a problem, when
> n<0 (I
> > > > >would like to decrease the date.s by one month). I still have no
> Idea how
> > > > to
> > > > >implement it.
> > > > >
> > > > >Best regards,
> > > > >Wojtek
> > > > >
> > > > >
> > > > >2006/2/5, Wojciech Slusarski <[hidden email]>:
> > > > >
> > > > >
> > > > >>Well,
> > > > >>
> > > > >>Thanks for the help, but I thought about something more general,
> that
> > > > >>could allow me adding n months to a date. I am sorry, that my
> question
> > > > >>wasn't so clear. If someone needed, here's the code:
> > > > >>
> > > > >>
> > > > >>EMONTH =
> > > > >>function(date.s, n)
> > > > >>{
> > > > >>  #function adds 'n' months to 'date.s'
> > > > >>  dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"),
> > > > length.out= n+1, by = "month")
> > > > >>  if(atoms(date.s)[1,"d"]>28)
> > > > >>  {
> > > > >>      if(atoms(dates)[n+1,"d"]<4)
> > > > >>   {
> > > > >>      emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
> > > > >>   } else {
> > > > >>     emonth = dates[n+1]
> > > > >>   }
> > > > >>  } else {
> > > > >>    emonth = dates[n+1]
> > > > >>  }
> > > > >>  emonth
> > > > >>}
> > > > >>
> > > > >>
> > > > >>
> > > > >># test
> > > > >>
> > > > >>date.s = timeDate("2005-12-30")
> > > > >>
> > > > >>
> > > > >>
> > > > >>>EMONTH(date.s, 2)@Data
> > > > >>>
> > > > >>>
> > > > >>[1] "2006-02-28"
> > > > >>Best regards,
> > > > >>Wojtek
> > > > >>
> > > > >>
> > > > >>2006/2/5, Diethelm Wuertz <[hidden email]>:
> > > > >>
> > > > >>
> > > > >>>Gabor Grothendieck wrote:
> > > > >>>
> > > > >>>
> > > > >>>
> > > > >>>>Try this:
> > > > >>>>
> > > > >>>>library(fCalendar)
> > > > >>>>dates <- timeSequence(from = "2005-12-1", to = "2006-12-1",
> format =
> > > > >>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > > > >>>>dates <- timeLastDayInMonth(dates)
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>Alternatively you can use
> > > > >>>
> > > > >>>
> > > > >>>
> > > > >>>>timeSequence(from = "2006-01-01", to = "2007-01-01", format =
> > > > >>>>
> > > > >>>>
> > > > >>>+ "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
> > > > >>>[1] "Europe/Warsaw"
> > > > >>>[1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31]
> [2006-04-30]
> > > > >>>[6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31]
> [2006-09-30]
> > > > >>>[11] [2006-10-31] [2006-11-30] [2006-12-31]
> > > > >>>
> > > > >>>Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5
> !
> > > > >>>
> > > > >>>DW
> > > > >>>
> > > > >>>
> > > > >>>
> > > > >>>>On 2/4/06, Wojciech Slusarski < [hidden email]>
> wrote:
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>>>Dear R users,
> > > > >>>>>
> > > > >>>>>Could anyone help me with creating a monthly series of dates?
> > > > >>>>>When I try to do:
> > > > >>>>>
> > > > >>>>>dates = timeSequence(from = "2005-12-31", to = "2006-12-31",
> format =
> > > > >>>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> > > > >>>>>
> > > > >>>>>I get:
> > > > >>>>>
> > > > >>>>>
> > > > >>>>>
> > > > >>>>>
> > > > >>>>>
> > > > >>>>>>dates
> > > > >>>>>>
> > > > >>>>>>
> > > > >>>>>>
> > > > >>>>>>
> > > > >>>>>[1] "Europe/Warsaw"
> > > > >>>>>[1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31]
> [2006-05-01]
> > > > >>>>>[6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31]
> [2006-10-01]
> > > > >>>>>[11] [2006-10-31] [2006-12-01] [2006-12-31]
> > > > >>>>>
> > > > >>>>>Unfourtunately, I would like to heave end of month at each
> point.
> > > > >>>>>I thought, that maybe using this function would also help when
> trying
> > > > >>>>>
> > > > >>>>>
> > > > >>>to add
> > > > >>>
> > > > >>>
> > > > >>>>>a month to a date. I would like to have
> > > > >>>>>
> > > > >>>>>"2006-01-31" + 1M = "2006-02-28"
> > > > >>>>>
> > > > >>>>>in MS EXCEL I would use EDATE("2006-01-31";1), and that would
> work. I
> > > > >>>>>couldn't find any similar function in R for that. Am I missing
> some
> > > > >>>>>function?
> > > > >>>>>
> > > > >>>>>Best regards,
> > > > >>>>>Wojtek
> > > > >>>>>
> > > > >>>>>      [[alternative HTML version deleted]]
> > > > >>>>>
> > > > >>>>>_______________________________________________
> > > > >>>>>[hidden email] mailing list
> > > > >>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > > >>>>>
> > > > >>>>>
> > > > >>>>>
> > > > >>>>>
> > > > >>>>>
> > > > >>>>_______________________________________________
> > > > >>>>[hidden email] mailing list
> > > > >>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>_______________________________________________
> > > > >>>[hidden email] mailing list
> > > > >>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > > >>>
> > > > >>>
> > > > >>>
> > > > >>
> > > > >>
> > > > >
> > > > >       [[alternative HTML version deleted]]
> > > > >
> > > > >_______________________________________________
> > > > >[hidden email] mailing list
> > > > >https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > > _______________________________________________
> > > [hidden email] mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > >
> >
>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Problems with monthly sequences

davidr-2
In reply to this post by Wojciech Slusarski
I thought the original poster might want end-end or modified following:
see http://www.bba.org.uk/bba/jsp/polopoly.jsp?d=225&a=1412 .
(It just helps to use the technical jargon so we are all clear.)

In any case, Gabor's function seems to satisfy.

David L. Reiner
Rho Trading Securities, LLC
 

> -----Original Message-----
> From: [hidden email] [mailto:r-sig-finance-
> [hidden email]] On Behalf Of Franklin Parlamis
> Sent: Sunday, February 05, 2006 3:50 PM
> To: Wojciech Slusarski
> Cc: r-sig-finance
> Subject: Re: [R-sig-finance] Problems with monthly sequences
>
> you want end.end convention (like for the interbank deposits
underlying
> BBA Libor quotations).  here's some code I wrote recently.  caveats:
>
> (i)  i just learned to code, so this probably doesn't exhibit
professional
> programming standards
> (ii) you need the Methods package (which is default loaded when R
opens)
> (iii) the functions are not yet complete (in that i don't deal with
> improper arguments as fully as i intend to)
> (iv) you will need to separately check whether the output dates are
> business days, and then apply the mod.following convention or
whichever
> other convention you want; all these functions do is move you forward
an
> increment subject to the end.end standard.
>
> (v) the date arguments are not character vectors as you envision;
rather
> they are objects that can be compelled to POSIXlt dates via
as.POSIXlt, or
> timeDate objects (you could easily add a method to convert the
character
> vectors to POSIXlt objects or timeDate objects)
> (vi) i'm pretty sure these functions won't work for negative
increments as
> you request, but I thank you for pointing out that functionality, as
it

> shouldn't be hard for me to alter the functions to get there.
>
> ----
>
> ########################
> ## last.of
> ########################
>
> last.of <- function(date) {
>   date<-as.POSIXlt(date)
>
>
switch(date[["mon"]]+1,31,28+leap.year(date[["year"]]+1900),31,30,31,30,
31

> ,31,30,31,30,31)}
>
> setMethod("last.of", signature(date="timeDate"), function(date)
>   last.of(date@Data))
>
> is.last.of <- function(date) {
>   date<-as.POSIXlt(date)
>   date[["mday"]]==last.of(date)}
>
> setMethod("is.last.of", signature(date="timeDate"), function(date)
>   is.last.of(date@Data))
>
> ########################
> ## end.end
> ########################
>
> end.end <- function(date, num, inc) {
>   date<-as.POSIXlt(date)
>   trial<-as.POSIXlt(seq(from=date,len=2,by=paste(num,inc))[2])
>   if(inc=="month") {
>     while((trial[["mon"]]-date[["mon"]])%%12>num)
>       trial<-as.POSIXlt(seq(from=trial,len=2,by="-1 day")[2])
>     if(is.last.of(date)) while(is.last.of(trial)==FALSE)
>       trial<-as.POSIXlt(seq(from=trial,len=2,by="1 day")[2])}
>   if(inc=="year") {
>     while(trial[["mon"]]!=date[["mon"]])
>       trial<-as.POSIXlt(seq(from=trial,len=2,by="-1 day")[2])
>     if(is.last.of(date)) while(is.last.of(trial)==FALSE)
>       trial<-as.POSIXlt(seq(from=trial,len=2,by="1 day")[2])}
>   trial}
>
> setMethod("end.end", signature(date="timeDate"), function(date, num,
inc)

>   end.end(date@Data, num, inc))
>
> -----
>
> franklin parlamis
>
> On Sunday, February 05, 2006, at 12:18PM, Wojciech Slusarski
> <[hidden email]> wrote:
>
> >Well, thanks for the help, but not exactly. The function I would like
to
> >have would give results for starting dates
> >
> >charvec = c("2005-12-25", "2006-01-30")
> >
> >>function_name(charvec,+1M)
> >
> >[1] [2006-01-25] [2006-02-28]
> >
> >So it's adding one month to a date, but when it exceeds the last day
of

> the
> >next month, it gives the las day.
> >
> >The other way could be also useful:
> >
> >charvec = c("2005-12-25", "2006-02-28")
> >
> >>function_name(charvec,-1M)
> >
> >[1] [2005-11-25] [2006-01-31]
> >#first date was decreased by one month, the second was decreased to
the
> last
> >day of january (not 28th January).
> >
> >Having such functions, one could set up proper dates for tenors in
> discount
> >curve. In the function you published few days ago, you setup fixed
year
> >fractions for each tenor, which don't take into account holidays etc.
> >
> >Best regards,
> >Wojtek
> >
> >2006/2/5, Diethelm Wuertz <[hidden email]>:
> >>
> >>
> >> Here comes a function which computes  time and date for the last
Day

> >> n-month ahead.
> >> It is Rmetrics conform and preserves time zone, daylight saving and
> >> Financial Centers.
> >>
> >> Is that what you wanted?
> >>
> >> DW
> >>
> >>
> >>
> >> timeLastDayNMonthAhead =
> >> function(charvec, month, format = "%Y-%m-%d", FinCenter = "GMT")
> >> {
> >>    if (Sys.getenv("TZ")[[1]] != "GMT") warning("Set timezone to
GMT!")
> >>    if (FinCenter == "") FinCenter = "GMT"
> >>    if (month < 0) stop("month must be positive")
> >>    date = timeLastDayInMonth(charvec, format = format, FinCenter =
> >> FinCenter)
> >>    for (i in 1:month) date = timeLastDayInMonth(date, format =
format,

> >>        FinCenter = FinCenter) + 3600*24
> >>
> >>    # Return Value:
> >>    timeLastDayInMonth(date, format = format, FinCenter = FinCenter)
> >> }
> >>
> >> # Example:
> >> > timeLastDayNMonthAhead(c("2005-12-25", "2006-02-28"), month = 2)
> >> [1] "GMT"
> >> [1] [2006-02-28] [2006-04-30]
> >>
> >>
> >>
> >>
> >>
> >> Wojciech Slusarski wrote:
> >>
> >> >I thought a bit about the function below, and see a problem, when
n<0
> (I
> >> >would like to decrease the date.s by one month). I still have no
Idea

> how
> >> to
> >> >implement it.
> >> >
> >> >Best regards,
> >> >Wojtek
> >> >
> >> >
> >> >2006/2/5, Wojciech Slusarski <[hidden email]>:
> >> >
> >> >
> >> >>Well,
> >> >>
> >> >>Thanks for the help, but I thought about something more general,
that

> >> >>could allow me adding n months to a date. I am sorry, that my
> question
> >> >>wasn't so clear. If someone needed, here's the code:
> >> >>
> >> >>
> >> >>EMONTH =
> >> >>function(date.s, n)
> >> >>{
> >> >>  #function adds 'n' months to 'date.s'
> >> >>  dates = timeSequence(from = format(date.s@Data, "%Y-%m-%d"),
> >> length.out= n+1, by = "month")
> >> >>  if(atoms(date.s)[1,"d"]>28)
> >> >>  {
> >> >>      if(atoms(dates)[n+1,"d"]<4)
> >> >>   {
> >> >>      emonth = dates[n+1]-atoms(dates)[n+1,"d"]*24*3600
> >> >>   } else {
> >> >>     emonth = dates[n+1]
> >> >>   }
> >> >>  } else {
> >> >>    emonth = dates[n+1]
> >> >>  }
> >> >>  emonth
> >> >>}
> >> >>
> >> >>
> >> >>
> >> >># test
> >> >>
> >> >>date.s = timeDate("2005-12-30")
> >> >>
> >> >>
> >> >>
> >> >>>EMONTH(date.s, 2)@Data
> >> >>>
> >> >>>
> >> >>[1] "2006-02-28"
> >> >>Best regards,
> >> >>Wojtek
> >> >>
> >> >>
> >> >>2006/2/5, Diethelm Wuertz <[hidden email]>:
> >> >>
> >> >>
> >> >>>Gabor Grothendieck wrote:
> >> >>>
> >> >>>
> >> >>>
> >> >>>>Try this:
> >> >>>>
> >> >>>>library(fCalendar)
> >> >>>>dates <- timeSequence(from = "2005-12-1", to = "2006-12-1",
format

> =
> >> >>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> >> >>>>dates <- timeLastDayInMonth(dates)
> >> >>>>
> >> >>>>
> >> >>>>
> >> >>>>
> >> >>>Alternatively you can use
> >> >>>
> >> >>>
> >> >>>
> >> >>>>timeSequence(from = "2006-01-01", to = "2007-01-01", format =
> >> >>>>
> >> >>>>
> >> >>>+ "%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw") - 3600*24
> >> >>>[1] "Europe/Warsaw"
> >> >>>[1] [2005-12-31] [2006-01-31] [2006-02-28] [2006-03-31]
[2006-04-30]
> >> >>>[6] [2006-05-31] [2006-06-30] [2006-07-31] [2006-08-31]
[2006-09-30]
> >> >>>[11] [2006-10-31] [2006-11-30] [2006-12-31]
> >> >>>
> >> >>>Please use proper ISO-8601 notation: 2006-01-05 and not 2006-1-5
!

> >> >>>
> >> >>>DW
> >> >>>
> >> >>>
> >> >>>
> >> >>>>On 2/4/06, Wojciech Slusarski < [hidden email]>
> wrote:
> >> >>>>
> >> >>>>
> >> >>>>
> >> >>>>
> >> >>>>>Dear R users,
> >> >>>>>
> >> >>>>>Could anyone help me with creating a monthly series of dates?
> >> >>>>>When I try to do:
> >> >>>>>
> >> >>>>>dates = timeSequence(from = "2005-12-31", to = "2006-12-31",
> format =
> >> >>>>>"%Y-%m-%d", by="month", FinCenter = "Europe/Warsaw")
> >> >>>>>
> >> >>>>>I get:
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>>>dates
> >> >>>>>>
> >> >>>>>>
> >> >>>>>>
> >> >>>>>>
> >> >>>>>[1] "Europe/Warsaw"
> >> >>>>>[1] [2005-12-31] [2006-01-31] [2006-03-03] [2006-03-31]
[2006-05-
> 01]
> >> >>>>>[6] [2006-05-31] [2006-07-01] [2006-07-31] [2006-08-31]
[2006-10-
> 01]
> >> >>>>>[11] [2006-10-31] [2006-12-01] [2006-12-31]
> >> >>>>>
> >> >>>>>Unfourtunately, I would like to heave end of month at each
point.

> >> >>>>>I thought, that maybe using this function would also help when
> trying
> >> >>>>>
> >> >>>>>
> >> >>>to add
> >> >>>
> >> >>>
> >> >>>>>a month to a date. I would like to have
> >> >>>>>
> >> >>>>>"2006-01-31" + 1M = "2006-02-28"
> >> >>>>>
> >> >>>>>in MS EXCEL I would use EDATE("2006-01-31";1), and that would
> work. I
> >> >>>>>couldn't find any similar function in R for that. Am I missing
> some
> >> >>>>>function?
> >> >>>>>
> >> >>>>>Best regards,
> >> >>>>>Wojtek
> >> >>>>>
> >> >>>>>      [[alternative HTML version deleted]]
> >> >>>>>
> >> >>>>>_______________________________________________
> >> >>>>>[hidden email] mailing list
> >> >>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>_______________________________________________
> >> >>>>[hidden email] mailing list
> >> >>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >> >>>>
> >> >>>>
> >> >>>>
> >> >>>>
> >> >>>>
> >> >>>_______________________________________________
> >> >>>[hidden email] mailing list
> >> >>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >> >>>
> >> >>>
> >> >>>
> >> >>
> >> >>
> >> >
> >> >       [[alternative HTML version deleted]]
> >> >
> >> >_______________________________________________
> >> >[hidden email] mailing list
> >> >https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >> >
> >> >
> >> >
> >>
> >>
> >
> > [[alternative HTML version deleted]]
> >
> >_______________________________________________
> >[hidden email] mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >
> >
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance