[R] Looking for a special date function in R

Sergey Goriatchev sergeyg at gmail.com
Wed Jan 21 13:08:56 CET 2009


Dear Prof. Ripley,

Thank you for help.
Yes, that is an interesting question you pose. I already thought
myself how February should be handled, as EDATE(31.08.2008; -6)
returns 29.02.2008.
In Excel it is not a problem, since this nonexisting date is then used
in VLOOKUP function where one can have Range.Lookup argument set to
TRUE and then it finds the closest value.
Example from Excel (value for 29.02.2008 is recovered from the sorted
table with VLOOKUP function):


21.07.2008	10.00
31.08.2008	20.00
28.02.2008	30.00
01.03.2008	40.00
02.03.2008	50.00
03.03.2008	60.00

29.02.2008	30

I wonder if that kind of functionality is available in R, or one has
to write a piece of code oneself to acheive it.

Thank you for your time and help!

Regards,
Sergey
			


On Wed, Jan 21, 2009 at 12:44, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> On Wed, 21 Jan 2009, Sergey Goriatchev wrote:
>
>> Dear Gabor,
>>
>> Thanks for that!
>> Still, it is not really similar to how EDATE works.
>> With julian(Sys.Date(), Sys.Date() - 10)  one moves 10 days back.
>> The problem is that I need to move by months, not by days, as months
>> have different number of days.
>> I need to come to the same day when I move backward or forward in
>> time, for example going back one month from today (21.01.2009) I need
>> to come to 21.12.2008.
>> I've read through your article in RNews 4/1 but still do not know how
>> to do what I need to do.
>
> The trick is to use the POSIXlt class.  E.g.
>
>> x <- Sys.Date()
>> xx <- as.POSIXlt(x)
>> xx$mon <- xx$mon - 6
>> as.Date(xx)
>
> [1] "2008-07-21"
>
> Now, the issue is what date is 6 months before 2008-08-31, and I'll leave
> you to ponder what that means.
>
>>
>> Regards,
>> Sergey
>>
>> On Wed, Jan 21, 2009 at 10:44, Gabor Grothendieck
>> <ggrothendieck at gmail.com> wrote:
>>>
>>> See ?julian
>>>
>>>> julian(Sys.Date(), Sys.Date() - 10) # 10
>>>
>>> [1] 10
>>> attr(,"origin")
>>> [1] "2009-01-11"
>>>
>>> and R News 4/1.
>>>
>>> On Wed, Jan 21, 2009 at 4:37 AM, Sergey Goriatchev <sergeyg at gmail.com>
>>> wrote:
>>>>
>>>> Hello, everyone
>>>>
>>>> I wonder if R has something similar to Excel function
>>>> EDATE(start_date; months) which returns a serial number of the date
>>>> that is the indicated number of months before of after the start date.
>>>> Example (the second column EDATE(first_column; -6)):
>>>> 01.01.1999      01.07.1998
>>>> 02.02.1999      02.08.1998
>>>> 06.03.1999      06.09.1998
>>>>
>>>> I am working with a zoo object where the row names are dates and for
>>>> particular rows I need to find values that were recorded 6 months
>>>> before (or return NA if the date is before the timeseries start).
>>>>
>>>> Maybe someone knows a passable R function for that kind of operation?
>>>>
>>>> Thanks in advance for help!
>>>>
>>>> Best,
>>>> Sergey
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>
>>
>>
>>
>> --
>> I'm not young enough to know everything. /Oscar Wilde
>> Experience is one thing you can't get for nothing. /Oscar Wilde
>> When you are finished changing, you're finished. /Benjamin Franklin
>> Tell me and I forget, teach me and I remember, involve me and I learn.
>> /Benjamin Franklin
>> Luck is where preparation meets opportunity. /George Patten
>>
>> ______________________________________________
>> 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.
>>
>
> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>



-- 
I'm not young enough to know everything. /Oscar Wilde
Experience is one thing you can't get for nothing. /Oscar Wilde
When you are finished changing, you're finished. /Benjamin Franklin
Tell me and I forget, teach me and I remember, involve me and I learn.
/Benjamin Franklin
Luck is where preparation meets opportunity. /George Patten




More information about the R-help mailing list