Create a time interval of 15 minutes from minutely data in R?

后端 未结 2 853
抹茶落季
抹茶落季 2020-12-03 03:47

I have some data which is formatted in the following way:

time     count 
00:00    17
00:01    62
00:02    41

So I have from 00:00 to 23:5

2条回答
  •  甜味超标
    2020-12-03 04:21

    The cut approach is handy but slow with large data frames. The following approach is approximately 1,000x faster than the cut approach (tested with 400k records.)

      #     Function: Truncate (floor) POSIXct to time interval (specified in seconds)
      #       Author: Stephen McDaniel @ PowerTrip Analytics
      #        Date : 2017MAY
      #    Copyright: (C) 2017 by Freakalytics, LLC
      #      License: MIT
    
      floor_datetime <- function(date_var, floor_seconds = 60, 
            origin = "1970-01-01") { # defaults to minute rounding
         if(!is(date_var, "POSIXct")) stop("Please pass in a POSIXct variable")
         if(is.na(date_var)) return(as.POSIXct(NA)) else {
            return(as.POSIXct(floor(as.numeric(date_var) / 
               (floor_seconds))*(floor_seconds), origin = origin))
         }
      }
    

    Sample output:

    test <- data.frame(good = as.POSIXct(Sys.time()), 
       bad1 = as.Date(Sys.time()),
       bad2 = as.POSIXct(NA))
    
    test$good_15 <- floor_datetime(test$good, 15 * 60)
    test$bad1_15 <- floor_datetime(test$bad1, 15 * 60)
    Error in floor_datetime(test$bad, 15 * 60) : 
      Please pass in a POSIXct variable
    test$bad2_15 <- floor_datetime(test$bad2, 15 * 60)
    
    test
    
                            good       bad1 bad2             good_15 bad2_15
        1 2017-05-06 13:55:34.48 2017-05-06  2007-05-06 13:45:00    
    

提交回复
热议问题