-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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