Skip to content

Instantly share code, notes, and snippets.

@Protonk
Forked from iros/timefill.R
Last active December 11, 2015 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Protonk/4616782 to your computer and use it in GitHub Desktop.
Save Protonk/4616782 to your computer and use it in GitHub Desktop.
slight update to padded time series post http://weblog.bocoup.com/padding-time-series-with-r/
# if we have the data in a csv, we can make it more reproducible
# with dput(), specifically
# dput(read.csv("~/Desktop/ts.csv", as.is = TRUE), control = NULL)
# there are some peculiarities with list type objects and dput
unpadded.ts <- data.frame(list(time = c("2011/11/01", "2012/01/01", "2011/12/01", "2012/06/01"), observations = c(12, 320, 100, 7)))
# specifying the format will save you
unpadded.ts$time <- as.Date(unpadded.ts$time, format = "%Y/%m/%d")
# we can generate the padded ts pretty directly
# the dreaded recycling rule appears here.
padded.ts <- data.frame(time = seq(min(unpadded.ts$time), max(unpadded.ts$time), by = "month"),
observations = 0)
# some subsetting and assignment and we're done!
padded.ts[padded.ts[, "time"] %in% unpadded.ts[, "time"], "observations"] <- unpadded.ts[, "observations"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment