Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #####
- # tt_add_missing_rows()
- # Many rolling functions require an integer input for the length of periods
- # to roll over. Missing data may cause differencing or other to have strange
- # behavior because it specifies integer 365 as a year, but with missing data
- # there are only 200 days in one year and 360 in another year and 355 in another
- # year.
- # In order to make sure the integer length of each period is correct, this
- # function adds missing rows.
- # User should also make sure there are not extra rows using tt_period_apply().
- #####
- require('dplyr')
- require('tibbletime')
- require('rlang')
- tt_add_missing_rows <- function(in.tbl_time, by = 'hour') {
- # Programatically find the index column
- index_column <- attributes(in.tbl_time)$index_quo[[2]]
- # Programatically find the first and last date/time in the index
- start.idx <- start(in.tbl_time[,paste0(index_column)][[1]])
- end.idx <- end(in.tbl_time[,paste0(index_column)][[1]])
- start <- in.tbl_time[start.idx,paste0(index_column)][[1]][[1]]
- end <- in.tbl_time[end.idx,paste0(index_column)][[1]][[1]]
- seq.vec <- seq(start,end,by=by)
- seq.df <- data.frame(seq.vec)
- # Make the date/time column the same as for the other vector
- # so that merge by will auto detect.
- names(seq.df)[1] <- paste(index_column)
- out.tbl_time <- merge(in.tbl_time,seq.df,all = TRUE)
- }
Add Comment
Please, Sign In to add comment