Guest User

Untitled

a guest
Feb 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #####
  2. # tt_add_missing_rows()
  3. # Many rolling functions require an integer input for the length of periods
  4. # to roll over. Missing data may cause differencing or other to have strange
  5. # behavior because it specifies integer 365 as a year, but with missing data
  6. # there are only 200 days in one year and 360 in another year and 355 in another
  7. # year.
  8. # In order to make sure the integer length of each period is correct, this
  9. # function adds missing rows.
  10. # User should also make sure there are not extra rows using tt_period_apply().
  11. #####
  12.  
  13. require('dplyr')
  14. require('tibbletime')
  15. require('rlang')
  16.  
  17. tt_add_missing_rows <- function(in.tbl_time, by = 'hour') {
  18. # Programatically find the index column
  19. index_column <- attributes(in.tbl_time)$index_quo[[2]]
  20.  
  21. # Programatically find the first and last date/time in the index
  22. start.idx <- start(in.tbl_time[,paste0(index_column)][[1]])
  23. end.idx <- end(in.tbl_time[,paste0(index_column)][[1]])
  24.  
  25. start <- in.tbl_time[start.idx,paste0(index_column)][[1]][[1]]
  26. end <- in.tbl_time[end.idx,paste0(index_column)][[1]][[1]]
  27.  
  28. seq.vec <- seq(start,end,by=by)
  29. seq.df <- data.frame(seq.vec)
  30. # Make the date/time column the same as for the other vector
  31. # so that merge by will auto detect.
  32. names(seq.df)[1] <- paste(index_column)
  33.  
  34. out.tbl_time <- merge(in.tbl_time,seq.df,all = TRUE)
  35. }
Add Comment
Please, Sign In to add comment