Skip to content

Instantly share code, notes, and snippets.

@michiel
Last active December 21, 2015 09:24
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 michiel/fba72389acf768baee65 to your computer and use it in GitHub Desktop.
Save michiel/fba72389acf768baee65 to your computer and use it in GitHub Desktop.
Pad timeseries data in R
# 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