[R] Date Math
josh.m.ulrich at gmail.com
Mon Oct 15 13:46:52 CEST 2012
On Sun, Oct 14, 2012 at 12:14 PM, Noah Silverman <noahsilverman at ucla.edu> wrote:
> I have a time series object (xts) that I iterate over in a loop. Works fine.
> My challenge is that I want to be able to reference other entries in the series by math. i.e. For today's observation, what were the last 5 observations? If indexed numerically, it is trivial, but I can figure out how to do this with dates.
The for loop iterator can only be an atomic vector:
for(i in Sys.time()+1:5) print(i)
for(i in Sys.Date()+1:5) print(i)
So how are you iterating over dates?
> This is slightly more difficult as there may not be an observation for every day. So I might want the last 5 that exist in the table, not the last 5 calendar days.
> ideally, it would be something like this.
> observations[ index(today)-5:today, ]
> However that obviously fails.
Since you can't iterate over dates/times anyway, just iterate over the
xts index and what you want to do is trivial (as you said).
> Noah Silverman, M.S.
> UCLA Department of Statistics
> 8117 Math Sciences Building
> Los Angeles, CA 90095
Joshua Ulrich | about.me/joshuaulrich
FOSS Trading | www.fosstrading.com
More information about the R-help