Last active
December 21, 2015 09:24
-
-
Save michiel/fba72389acf768baee65 to your computer and use it in GitHub Desktop.
Pad timeseries data in R
This file contains hidden or 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
# based on https://bocoup.com/weblog/padding-time-series-with-r | |
pad_data <- function(data, attr='date', value='value', by='hour') { | |
sorted.data <- data[order(data[[attr]]),] | |
info.data.length <- length(sorted.data[[attr]]) | |
info.date.min <- sorted.data[[attr]][1] | |
info.date.max <- sorted.data[[attr]][info.data.length] | |
all.dates <- seq(info.date.min, info.date.max, by=by) | |
all.dates.frame <- data.frame(list(time=all.dates)) | |
merged.data <- merge(all.dates.frame, sorted.data, all=T) | |
merged.data[[value]][which(is.na(merged.data[[value]]))] <- 0 | |
merged.data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment