[R] A Date related problem

Marc Schwartz marc_schwartz at me.com
Tue Dec 15 21:22:12 CET 2009


On Dec 15, 2009, at 2:19 PM, Marc Schwartz wrote:

> On Dec 15, 2009, at 1:39 PM, Megh wrote:
>
>>
>> Dear all,
>>
>> Please consider following date "as.Date("2009-02-01")". If I  
>> subtract "1"
>> then it will give last day, similarly if I subtract "2" it will  
>> give 2nd
>> last day. But what about if I want to get "last month", "2md last  
>> month"
>> i.e. "2009-01-01" or "2008-12-01" etc or even year?
>>
>> Is there any automated way (like day-case, as explained) for doing  
>> that?
>>
>> Your help will be highly appreciated.
>
>
> See ?seq.date

Typo:

Should be ?seq.Date

Marc

>
> You can use a negative value for the 'by' argument along with the  
> desired unit of time. Then define how many values in the sequence  
> you want. We'll also remove the first value, which is the starting  
> point.
>
> # The prior 2 months
>
> > seq(as.Date("2009-02-01"), by = "-1 month", length = 3)[-1]
> [1] "2009-01-01" "2008-12-01"
>
>
> # Prior two years
>
> > seq(as.Date("2009-02-01"), by = "-1 year", length = 3)[-1]
> [1] "2008-02-01" "2007-02-01"
>
>
> HTH,
>
> Marc Schwartz
>




More information about the R-help mailing list