I want to perform an identical calculation based on daily values prior to a different time slot and in a specific time slot. My dataset consists out of hourly data of users over the year 2016, an example set of a single user is provided below.
For each day in a year I want to aggregate the positive values between before 03:00 and subtract those from the aggregate values between 03:00 and 05:00 (regardless whether these values are positive of negative)
Example data of a single user below
Time_index value
2016-01-01 00:00:00 0.076805274
2016-01-01 01:00:00 -0.230500848
2016-01-01 02:00:00 -0.013729424
2016-01-01 03:00:00 -0.574499489
2016-01-01 04:00:00 -0.987048205
2016-01-01 05:00:00 0.172100756
2016-01-01 06:00:00 0.495357096
" "
2016-01-02 00:00:00 0.714050398
2016-01-02 01:00:00 0.205577513
2016-01-02 02:00:00 -0.223645255
2016-01-02 03:00:00 -0.562646915
2016-01-02 04:00:00 -0.420385944
2016-01-02 05:00:00 -0.548992675
2016-01-02 06:00:00 -0.732962989
time_index <- seq(from = as.POSIXct("2016-01-01 00:00"),
to = as.POSIXct("2016-12-31 23:00"),
by = "hour")
value <- runif(length(time_index), min = -1, max = 1)
example <- data.frame(time_index, value)
Based on the example data the calculation should look like this for January 1st:
Positive 00:00- 02:00 Everything 03:00 - 05:00
0.076805274 - (-0.574499489 - 0.987048205 + 0.172100756)
How do I perform this calculation that depends on (1) two time frames that can differ in time and (2) the numbers being positive for the first instance, across all days in a year? And if this is doable, how could it be scaled up to do it for multiple users (so different columns of values).
I have tried using nested for-loops and do the calculations based on if-else statements, but I have not been able to loop over the each individual day in combination with the right timeframes. Apart from that, since nested for-loops are notoriously slow in R, I am afraid that it will take too long to do it for all users.