Jonathan Greenberg
2010-Jun-29 21:58 UTC
[R] using zoo() to coerce time series to a different reference frame
Folks: I have two sets of dates, and one set of data: *** require("chron") require("zoo") reference_dates=seq.dates("01/01/92", "12/31/92", by = "months") data_dates=seq.dates("01/15/91", "12/15/93", by = "months") data=1:length(data_dates) reference_zoo=zoo(order.by=reference_dates) data_zoo=zoo(data,data_dates) *** What I would like is to have a zoo object that uses the index from reference_dates, but grabs the data for each of the dates (using a spline interpolation) from data_zoo object. I feel like my solution is a bit slow, can someone let me know if there is a quicker way to do this? Thanks: *** reference_data_zoo_merge=merge(reference_zoo,data_zoo) reference_data_zoo_data=na.spline(reference_data_zoo_merge) reference_data_zoo_data=merge(reference_zoo,reference_data_zoo_data,all=FALSE) *** --j
Achim Zeileis
2010-Jun-30 03:18 UTC
[R] using zoo() to coerce time series to a different reference frame
On Tue, 29 Jun 2010, Jonathan Greenberg wrote:> Folks: > > I have two sets of dates, and one set of data: > > *** > > require("chron") > require("zoo") > reference_dates=seq.dates("01/01/92", "12/31/92", by = "months") > data_dates=seq.dates("01/15/91", "12/15/93", by = "months") > data=1:length(data_dates) > > reference_zoo=zoo(order.by=reference_dates) > data_zoo=zoo(data,data_dates) > > *** > > What I would like is to have a zoo object that uses the index from > reference_dates, but grabs the data for each of the dates (using a > spline interpolation) from data_zoo object. I feel like my solution > is a bit slow, can someone let me know if there is a quicker way to do > this? Thanks:With current versions of "zoo" you can simply do: na.spline(data_zoo, xout = reference_dates) hth, Z> *** > > reference_data_zoo_merge=merge(reference_zoo,data_zoo) > reference_data_zoo_data=na.spline(reference_data_zoo_merge) > reference_data_zoo_data=merge(reference_zoo,reference_data_zoo_data,all=FALSE) > > *** > > --j > > ______________________________________________ > 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. >
Gabor Grothendieck
2010-Jun-30 03:20 UTC
[R] using zoo() to coerce time series to a different reference frame
On Tue, Jun 29, 2010 at 5:58 PM, Jonathan Greenberg <greenberg at ucdavis.edu> wrote:> Folks: > > I have two sets of dates, and one set of data: > > *** > > require("chron") > require("zoo") > reference_dates=seq.dates("01/01/92", "12/31/92", by = "months") > data_dates=seq.dates("01/15/91", "12/15/93", by = "months") > data=1:length(data_dates) > > reference_zoo=zoo(order.by=reference_dates) > data_zoo=zoo(data,data_dates) > > *** > > What I would like is to have a zoo object that uses the index from > reference_dates, but grabs the data for each of the dates (using a > spline interpolation) from data_zoo object. ?I feel like my solution > is a bit slow, can someone let me know if there is a quicker way to do > this? ?Thanks: > > *** > > reference_data_zoo_merge=merge(reference_zoo,data_zoo) > reference_data_zoo_data=na.spline(reference_data_zoo_merge) > reference_data_zoo_data=merge(reference_zoo,reference_data_zoo_data,all=FALSE) >Try this:> na.spline(data_zoo, xout = reference_dates)01/01/92 02/01/92 03/01/92 04/01/92 05/01/92 06/01/92 07/01/92 08/01/92 09/01/92 10/01/92 11/01/92 12/01/92 12.55383 13.53979 14.51858 15.55116 16.53268 17.54855 18.53231 19.55283 20.54369 21.53461 22.54817 23.53190