[R] Date Math

Paul Gilbert pgilbert902 at gmail.com
Mon Oct 15 15:17:36 CEST 2012



On 12-10-15 06:00 AM, r-help-request at r-project.org wrote:
> Jeff,
>
> My understanding is that the lag command will lag an entire time series.  That isn't what I'm looking for.
>
> I just want, for example, today, and 5 entries back.
>
> for exmple:
>
> iter <- '2011-05-18'
>
> observations[iter]   # works fine, returns the row at that date.
>
> index(observations[iter[) # just returns the iter back
>
> index(observations[iter]) - 5  # returns "2011-05-17 23:59:57 PDT", so it subtracted 3 seconds.
>
> really, I want to find:  iter- 5 days.

Switching between 5 entries back and 5 days back has me confused about 
whether you have more than one entry some days or not. If you mean you 
have at most one entry per day, and you want the last five days that you 
have observations, then this part of your problem can be solved by 
switching from POSIXct to dates:
 >  z <-  Sys.time()
 > z
[1] "2012-10-15 08:34:58 EDT"
 > z -5
[1] "2012-10-15 08:34:53 EDT"
 > as.Date(z) -5
[1] "2012-10-10"

(BTW, a complete example is always useful when asking for help. I think 
your "obviously fails" code would be pretty close to working, depending 
on how the dates were generated.)

Paul
>
>
>
> --
> Noah Silverman
> Smart Media Corp.




More information about the R-help mailing list