Murali.MENON at fortisinvestments.com
2009-Apr-27 10:51 UTC
[R] series at low freq expanded into high freq
Folks, If I have a series mm of, say, monthly observations, and a series dd of daily dates, what's a good way of expanding mm such that corresponding to each day in dd within the corresponding month in mm, the values of mm are repeated? So e.g., if I have mm: mm <- c(15, 10, 12, 13, 11) names(mm)<-c("Nov 2008", "Dec 2008", "Jan 2009", "Feb 2009", "Mar 2009") library(zoo) mm <- zoo(mm, order.by = as.yearmon(names(mm), format="%b %Y")) And days: dd <- as.Date(c("03/11/2008", "05/11/2008", "04/01/2009","02/02/2009","17/02/2009","13/03/2009","14/03/2009","18/03/ 2009", "26/03/2009"), format="%d/%m/%Y") I want to be able to create a series that looks like this: 03/11/2008 15 05/11/2008 15 04/01/2009 12 02/02/2009 13 17/02/2009 13 13/03/2009 11 14/03/2009 11 18/03/2009 11 26/03/2009 11 where because no daily dates in dd are in December, the final series has no entries for December; there are two entries for November because there are two dates in dd in November, etc. Thanks, Murali
Murali.MENON at fortisinvestments.com
2009-Apr-27 11:55 UTC
[R] series at low freq expanded into high freq
Hmm, silly of me. I have the solution, after some mucking about with coercion and so on:> xx <- zoo(mm[as.character(as.yearmon(dd))], order.by = dd) > xx2008-11-03 2008-11-05 2009-01-04 2009-02-02 2009-02-17 2009-03-13 2009-03-14 2009-03-18 2009-03-26 15 15 12 13 13 11 11 11 11 Right. Sorry to waste your time. Murali -----Original Message----- From: MENON Murali Sent: 27 April 2009 11:51 To: 'r-help at r-project.org' Subject: series at low freq expanded into high freq Folks, If I have a series mm of, say, monthly observations, and a series dd of daily dates, what's a good way of expanding mm such that corresponding to each day in dd within the corresponding month in mm, the values of mm are repeated? So e.g., if I have mm: mm <- c(15, 10, 12, 13, 11) names(mm)<-c("Nov 2008", "Dec 2008", "Jan 2009", "Feb 2009", "Mar 2009") library(zoo) mm <- zoo(mm, order.by = as.yearmon(names(mm), format="%b %Y")) And days: dd <- as.Date(c("03/11/2008", "05/11/2008", "04/01/2009","02/02/2009","17/02/2009","13/03/2009","14/03/2009","18/03/ 2009", "26/03/2009"), format="%d/%m/%Y") I want to be able to create a series that looks like this: 03/11/2008 15 05/11/2008 15 04/01/2009 12 02/02/2009 13 17/02/2009 13 13/03/2009 11 14/03/2009 11 18/03/2009 11 26/03/2009 11 where because no daily dates in dd are in December, the final series has no entries for December; there are two entries for November because there are two dates in dd in November, etc. Thanks, Murali
Try this:> library(zoo) > mm <- c(15, 10, 12, 13, 11) > names(mm)<-c("Nov 2008", "Dec 2008", "Jan 2009", "Feb 2009", "Mar 2009") > library(zoo) > mm <- zoo(mm, order.by = as.yearmon(names(mm), format="%b %Y")) > > dd <- as.Date(c("03/11/2008", "05/11/2008",+ "04/01/2009","02/02/2009","17/02/2009","13/03/2009","14/03/2009","18/03/2009", "26/03/2009"), format="%d/%m/%Y")> > zoo(coredata(mm)[match(as.yearmon(dd), time(mm))], dd)2008-11-03 2008-11-05 2009-01-04 2009-02-02 2009-02-17 2009-03-13 2009-03-14 2009-03-18 2009-03-26 15 15 12 13 13 11 11 11 11 On Mon, Apr 27, 2009 at 6:51 AM, <Murali.MENON at fortisinvestments.com> wrote:> Folks, > > If I have a series mm of, say, monthly observations, and a series dd of > daily dates, what's a good way of expanding mm such that corresponding > to each day in dd within the corresponding month in mm, the values of mm > are repeated? > > So e.g., if I have mm: > > mm <- c(15, 10, 12, 13, 11) > names(mm)<-c("Nov 2008", ? "Dec 2008", ?"Jan 2009", ? "Feb 2009", ? "Mar > 2009") > library(zoo) > mm <- zoo(mm, order.by = as.yearmon(names(mm), format="%b %Y")) > > And days: > > dd <- as.Date(c("03/11/2008", "05/11/2008", > "04/01/2009","02/02/2009","17/02/2009","13/03/2009","14/03/2009","18/03/ > 2009", "26/03/2009"), format="%d/%m/%Y") > > I want to be able to create a series that looks like this: > > 03/11/2008 ? ? ? 15 > 05/11/2008 ? ? ? 15 > 04/01/2009 ? ? ? 12 > 02/02/2009 ? ? ? 13 > 17/02/2009 ? ? ? 13 > 13/03/2009 ? ? ? 11 > 14/03/2009 ? ? ? 11 > 18/03/2009 ? ? ? 11 > 26/03/2009 ? ? ? 11 > > where because no daily dates in dd are in December, the final series has > no entries for December; there are two entries for November because > there are two dates in dd in November, etc. > > Thanks, > Murali > > ______________________________________________ > 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. >