Any method to populate (pad) monthly zoo objects with signals on lower time-scale?

StackOverflow https://stackoverflow.com/questions/12947904

  •  08-07-2021
  •  | 
  •  

Frage

Suppose I have a monthly series of zoo data with signals from the set {0,1}.

e.g.

         a b c
Oct 2005 1 0 1
Nov 2005 0 1 1
Dec 2005 0 1 0
Jan 2006 1 0 0
Feb 2006 1 1 0
Mar 2006 0 0 1
Apr 2006 0 0 1
May 2006 0 1 1
Jun 2006 1 1 0
Jul 2006 0 1 1
Aug 2006 1 0 0
Sep 2006 0 1 1
Oct 2006 0 1 1
Nov 2006 1 0 0
Dec 2006 0 0 1
Jan 2007 1 1 1
Feb 2007 0 1 0
Mar 2007 1 1 0
Apr 2007 0 0 0
May 2007 1 0 0

Is there a clever vectorized approach to populate the daily data in-between months with the signal values? Also, it needs to be lag shifted by one. So for Row 1, Oct,2005. a,b,c= c(1,0,1), I would like to populate all of Nov 2005 with that set {1,0,1}. Next, would be Nov, 2005 = {0,1,1} and all of December would be populated with these values... and so on until the nth month.

I would prefer to start on a monthly scale to generate, but have the populated daily values to analyze. I'm thinking about something like auto-filtering or pivot-tables in excel.

War es hilfreich?

Lösung

This is actually fairly straight forward, thanks to zoo. First, convert the index of your object from yearmon to Date. Then generate a daily sequence of dates from the start to end of that series. Finally, merge and fill in missing values.

# assuming your monthly series is called 'Z'
# create new object and convert index to Date
z <- Z
index(z) <- as.Date(index(z))

# generate daily sequence
# note that yearmon are converted to the first Date of the month,
# so I add one month and subtract one day from the last yearmon value
dailySeq <- seq(start(z), as.Date(end(Z)+1/12)-1, by="1 day")

# merge z with empty zoo object containing the new daily index
d <- merge(z, zoo(,dailySeq))
# fill in missing values using last-observation-carried-forward
d <- na.locf(d)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top