Hello, 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. 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. Ideas? -- Noah Silverman, M.S. UCLA Department of Statistics 8117 Math Sciences Building Los Angeles, CA 90095
There are a few ways. The xts package has a "lag" function. So does "zoo". Pay careful attention to the conventions used for specifying relative time in these various packages. You can also infill your missing data to create a regularly-spaced time series. There is no shortage of web information about this topic... you can start at the Time Series task view on CRAN or just use a search engine. On Sun, 14 Oct 2012, Noah Silverman wrote:> Hello, > > > 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. > > 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. > > Ideas? > > > > -- > Noah Silverman, M.S. > UCLA Department of Statistics > 8117 Math Sciences Building > Los Angeles, CA 90095 > > ______________________________________________ > 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. >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Noah Silverman > Sent: Sunday, October 14, 2012 7:14 PM > To: R-help at r-project.org > Subject: [R] Date Math > > Hello, > > > 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. > > 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.I would say that you are looking for ?embed. However it does not give you observations from last five days but from any last 5 existing observations regardless of time span. Regards Petr> > Ideas? > > > > -- > Noah Silverman, M.S. > UCLA Department of Statistics > 8117 Math Sciences Building > Los Angeles, CA 90095 > > ______________________________________________ > 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.
Hi Noah, On Sun, Oct 14, 2012 at 12:14 PM, Noah Silverman <noahsilverman at ucla.edu> wrote:> Hello, > > > 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. > > Ideas? >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 >Best, -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com
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.