Reputation: 23
I have a number of start and end times. I assign the fraction of time to 15-minute bins (4 /hr). Each bin would contain 0, 15 or the <15 minutes that would be assigned to that bin. For example, Start 08:42 and end 09:37, would have bins 00:00-08:15 as 0, there would be a value of 12 in bin 08:30, values of 15 in 08:45-9:30, a value of 7 in bin 09:30 and bins 09:45-23:45 would have a value of 0.
arrival <- as.POSIXct(c("2021-03-27 02:41:00", "2021-03-27 05:23:00", "2021-03-27 05:59:00", "2021-03-27 07:04:00", "2021-03-27 08:10:00"))
depart <- as.POSIXct(c("2021-03-27 08:10:00", "2021-03-27 07:19:00", "2021-03-27 10:16:00", "2021-03-27 12:08:00", "2021-03-27 15:34:00"))
times <- data.frame(arrival,depart)
The desired output is for each arrival, departure pair, a vector 96 bins of values 0:15. I can see a way to do this with a for loop, was looking for a more elegant solution. Any thoughts? Thank you in advance.
Upvotes: 0
Views: 53
Reputation: 19375
I can see a way to do this with a for loop, was looking for a more elegant solution. Any thoughts?
Often we can use apply functions instead of loops, as you certainly know. E. g.:
# convert the POSIX times to the minutes of the day
dayminutes = mapply(function (r) (as.numeric(r)/60)%%(60*24), times)
# arrival depart
#[1,] 161 490
#[2,] 323 439
#[3,] 359 616
#[4,] 424 728
#[5,] 490 934
# use "cut" to divide the ranges arrival:(depart-1) into 15 min. intervals over the day
apply(dayminutes, 1, function(r) as.vector(summary(cut(r['arrival']:(r['depart']-1), 0:(4*24)*15, right=F))))
The result is:
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
[6,] 0 0 0 0 0
[7,] 0 0 0 0 0
[8,] 0 0 0 0 0
[9,] 0 0 0 0 0
[10,] 0 0 0 0 0
[11,] 4 0 0 0 0
[12,] 15 0 0 0 0
[13,] 15 0 0 0 0
[14,] 15 0 0 0 0
[15,] 15 0 0 0 0
[16,] 15 0 0 0 0
[17,] 15 0 0 0 0
[18,] 15 0 0 0 0
[19,] 15 0 0 0 0
[20,] 15 0 0 0 0
[21,] 15 0 0 0 0
[22,] 15 7 0 0 0
[23,] 15 15 0 0 0
[24,] 15 15 1 0 0
[25,] 15 15 15 0 0
[26,] 15 15 15 0 0
[27,] 15 15 15 0 0
[28,] 15 15 15 0 0
[29,] 15 15 15 11 0
[30,] 15 4 15 15 0
[31,] 15 0 15 15 0
[32,] 15 0 15 15 0
[33,] 10 0 15 15 5
[34,] 0 0 15 15 15
[35,] 0 0 15 15 15
[36,] 0 0 15 15 15
[37,] 0 0 15 15 15
[38,] 0 0 15 15 15
[39,] 0 0 15 15 15
[40,] 0 0 15 15 15
[41,] 0 0 15 15 15
[42,] 0 0 1 15 15
[43,] 0 0 0 15 15
[44,] 0 0 0 15 15
[45,] 0 0 0 15 15
[46,] 0 0 0 15 15
[47,] 0 0 0 15 15
[48,] 0 0 0 15 15
[49,] 0 0 0 8 15
[50,] 0 0 0 0 15
[51,] 0 0 0 0 15
[52,] 0 0 0 0 15
[53,] 0 0 0 0 15
[54,] 0 0 0 0 15
[55,] 0 0 0 0 15
[56,] 0 0 0 0 15
[57,] 0 0 0 0 15
[58,] 0 0 0 0 15
[59,] 0 0 0 0 15
[60,] 0 0 0 0 15
[61,] 0 0 0 0 15
[62,] 0 0 0 0 15
[63,] 0 0 0 0 4
[64,] 0 0 0 0 0
[65,] 0 0 0 0 0
[66,] 0 0 0 0 0
[67,] 0 0 0 0 0
[68,] 0 0 0 0 0
[69,] 0 0 0 0 0
[70,] 0 0 0 0 0
[71,] 0 0 0 0 0
[72,] 0 0 0 0 0
[73,] 0 0 0 0 0
[74,] 0 0 0 0 0
[75,] 0 0 0 0 0
[76,] 0 0 0 0 0
[77,] 0 0 0 0 0
[78,] 0 0 0 0 0
[79,] 0 0 0 0 0
[80,] 0 0 0 0 0
[81,] 0 0 0 0 0
[82,] 0 0 0 0 0
[83,] 0 0 0 0 0
[84,] 0 0 0 0 0
[85,] 0 0 0 0 0
[86,] 0 0 0 0 0
[87,] 0 0 0 0 0
[88,] 0 0 0 0 0
[89,] 0 0 0 0 0
[90,] 0 0 0 0 0
[91,] 0 0 0 0 0
[92,] 0 0 0 0 0
[93,] 0 0 0 0 0
[94,] 0 0 0 0 0
[95,] 0 0 0 0 0
[96,] 0 0 0 0 0
Upvotes: 0