R: Efficiently subsetting dataframe based on time of day

后端 未结 2 994
挽巷
挽巷 2021-01-06 07:43

I have a large (150,000x7) dataframe that I intend to use for back-testing and real-time analysis of a financial market. The data represents the condition of an investment v

2条回答
  •  佛祖请我去吃肉
    2021-01-06 08:16

    Say that you have your target time t0 on the same scale as pTime: seconds since epoch. Then t0 - pTime = (difference in the number of days since epoch between the two) + (difference in remaining seconds). Taking t0 - pTime %% (num. seconds per day) will leave us with the difference in seconds in clock arithmetic (wrapped around if the difference is negative). This suggests the following function:

    SecondsPerDay <- 24 * 60 * 60
    within <- function(d, t0Sec, wMin) {
      diff <- (d$pTime - t0Sec) %% SecondsPerDay
      wSec <- 60 * wMin
      return(d[diff < wSec | diff > (SecondsPerDay - wSec), ])
    }
    

提交回复
热议问题