[R-sig-finance] Problems with monthly sequences
Diethelm Wuertz
wuertz at itp.phys.ethz.ch
Sun Feb 5 20:52:25 CET 2006
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 <wojciech.slusarski at gmail.com>:
>
>
>>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 at 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 <wuertz at itp.phys.ethz.ch>:
>>
>>
>>>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 < wojciech.slusarski at gmail.com> 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]]
>>>>>
>>>>>_______________________________________________
>>>>>R-sig-finance at stat.math.ethz.ch mailing list
>>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>_______________________________________________
>>>>R-sig-finance at stat.math.ethz.ch mailing list
>>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>>
>>>>
>>>>
>>>>
>>>>
>>>_______________________________________________
>>>R-sig-finance at stat.math.ethz.ch mailing list
>>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>
>>>
>>>
>>
>>
>
> [[alternative HTML version deleted]]
>
>_______________________________________________
>R-sig-finance at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>
>
More information about the R-sig-finance
mailing list