Guest User

Untitled

a guest
Dec 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. # start "2017-10-28 00:00:00"
  2. # end "2017-10-28 23:59:00"
  3. # unit A character string, containing one of "sec", "min", "hour", "day", "DSTday", "week", "month", "quarter" or "year"
  4. interval_matrix <- function(df,interv,start,end,unit){
  5. interv <- enquo(interv)
  6.  
  7. time_cols <- seq.POSIXt(as_datetime(ymd_hms(start)),
  8. as_datetime(ymd_hms(end)),
  9. by="min")
  10.  
  11. time_str <- function(x){
  12. x %>% lubridate::year() %>% as.character() %>% str_pad(2,side="left",pad="0") -> yy
  13. x %>% lubridate::month() %>% as.character() %>% str_pad(2,side="left",pad="0") -> mo
  14. x %>% lubridate::day() %>% as.character() %>% str_pad(2,side="left",pad="0") -> dd
  15. x %>% lubridate::hour() %>% as.character() %>% str_pad(2,side="left",pad="0") -> hh
  16. x %>% lubridate::minute() %>% as.character() %>% str_pad(2,side="left",pad="0") -> mm
  17. x %>% lubridate::second() %>% as.character() %>% str_pad(2,side="left",pad="0") -> ss
  18. paste0(yy,"_",mo,"_",dd,"_",hh,"_",mm,"_",ss)
  19. }
  20.  
  21. ltc <- length(time_cols)
  22. for(i in 1:ltc){
  23. p <- time_str(time_cols[i])
  24. p <- paste0("time_",p)
  25. df[[p]] <- time_cols[i] %within% df[[interval]] # UNDER CONSTRUCTION - NEED TO TURN QUOSURE INTO STRING FOR THIS TO WORK
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32. }
Add Comment
Please, Sign In to add comment