I have a vector with minutes, seconds and fractions of seconds
tm <- structure(c("49:03.3", "49:05.5", "49:07.8", "49:10.1", "49:12.3", "49:14.6",
"49:16.8", "49:19.1", "49:21.4", "49:23.6", "49:25.9", "49:28.2",
"49:30.4", "49:32.7", "49:34.9", "49:37.2", "49:39.5", "49:41.7",
"49:44.0", "49:46.2", "49:48.5", "49:50.8", "49:53.0", "49:55.3",
"49:57.6", "49:59.8", "50:02.1", "50:04.4", "50:06.6", "50:08.9",
"50:11.2", "50:13.4", "50:15.7", "50:18.0", "50:20.2", "50:22.5",
"50:24.7", "50:27.0", "50:29.3", "50:31.5", "50:33.8", "50:36.0",
"50:38.3", "50:40.6", "50:42.8", "50:45.1", "50:47.4", "50:49.6",
"50:51.9", "50:54.1", "50:56.4", "50:58.7", "51:01.0", "51:03.2",
"51:05.5", "51:07.7", "51:10.0", "51:12.3", "51:14.5", "51:16.8",
"51:19.0", "51:21.4", "51:23.6", "51:25.9", "51:28.1", "51:30.4",
"51:32.7", "51:34.9", "51:37.2", "51:39.4", "51:41.7", "51:44.0",
"51:46.2", "51:48.5", "51:50.8", "51:53.0", "51:55.3", "51:57.5",
"51:59.8", "52:02.1", "52:04.3", "52:06.6", "52:08.8", "52:11.1",
"52:13.4", "52:15.6", "52:17.9", "52:20.2", "52:22.4", "52:24.7",
"52:26.9", "52:29.2", "52:31.5", "52:33.7", "52:36.0", "52:38.3",
"52:40.6", "52:42.8", "52:45.1", "52:47.3", "52:49.6", "52:51.9",
"52:54.1", "52:56.4", "52:58.6", "53:00.9", "53:03.2", "53:05.4",
"53:07.7", "53:10.0", "53:12.2", "53:14.5", "53:16.7", "53:19.0",
"53:21.3", "53:23.5", "53:25.8", "53:28.0", "53:30.3", "53:32.6",
"53:34.9", "53:37.1", "53:39.4", "53:41.6", "53:43.9", "53:46.2",
"53:48.4", "53:50.7", "53:52.9", "53:55.3", "53:57.5", "53:59.8",
"54:02.0", "54:04.3", "54:06.6", "54:08.8", "54:11.1", "54:13.3",
"54:15.6", "54:17.9", "54:20.1", "54:22.4", "54:24.7", "54:26.9",
"54:29.2", "54:31.4", "54:33.7", "54:36.0", "54:38.2", "54:40.5",
"54:42.8", "54:45.0", "54:47.3", "54:49.5", "54:51.8", "54:54.1",
"54:56.3", "54:58.6", "55:00.8", "55:03.1", "55:05.4", "55:07.6",
"55:09.9", "55:12.2", "55:14.5", "55:16.7", "55:19.0", "55:21.2",
"55:23.5", "55:25.8", "55:28.0", "55:30.3", "55:32.6", "55:34.8",
"55:37.1", "55:39.3", "55:41.6", "55:43.9", "55:46.1", "55:48.4",
"55:50.6", "55:52.9", "55:55.2", "55:57.5", "55:59.7", "56:02.0",
"56:04.2", "56:06.5", "56:08.8", "56:11.0", "56:13.3", "56:15.5",
"56:17.8", "56:20.1", "56:22.3", "56:24.6", "56:26.9", "56:29.2",
"56:31.4", "56:33.7", "56:35.9", "56:38.2", "56:40.5", "56:42.7",
"56:45.0", "56:47.3", "56:49.5", "56:51.8", "56:54.0", "56:56.3",
"56:58.6", "57:00.8", "57:03.1", "57:05.3", "57:07.6", "57:09.9",
"57:12.1", "57:14.4", "57:16.7", "57:18.9", "57:21.2", "57:23.4",
"57:25.7", "57:28.0", "57:30.2", "57:32.5", "57:34.7", "57:37.0",
"57:39.3", "57:41.5", "57:43.8", "57:46.1", "57:48.4", "57:50.6",
"57:52.9", "57:55.1", "57:57.4", "57:59.7", "58:01.9", "58:04.2",
"58:06.5", "58:08.7", "58:11.0", "58:13.2", "58:15.5", "58:17.8",
"58:20.1", "58:22.3", "58:24.5", "58:26.8", "58:29.1", "58:31.4",
"58:33.6", "58:35.9", "58:38.1", "58:40.4", "58:42.7", "58:44.9",
"58:47.2", "58:49.5", "58:51.7", "58:54.0", "58:56.2", "58:58.5",
"59:00.8", "59:03.1", "59:05.3", "59:07.6", "59:09.9", "59:12.1",
"59:14.4", "59:16.6", "59:18.9", "59:21.2", "59:23.4", "59:25.7",
"59:27.9", "59:30.2", "59:32.5", "59:34.7", "59:37.0", "59:39.3",
"59:41.5", "59:43.8", "59:46.0", "59:48.3", "59:50.6", "59:52.8",
"59:55.1", "59:57.3", "59:59.6", "00:01.9", "00:04.1", "00:06.4",
"00:08.7", "00:10.9", "00:13.2", "00:15.4", "00:17.7", "00:20.0",
"00:22.3", "00:24.5", "00:26.8", "00:29.1", "00:31.3", "00:33.6",
"00:35.8", "00:38.1", "00:40.4", "00:42.6", "00:44.9", "00:47.1",
"00:49.4", "00:51.7", "00:54.0", "00:56.2", "00:58.5", "01:00.7",
"01:03.0", "01:05.3", "01:07.5", "01:09.8", "01:12.0", "01:14.3",
"01:16.6", "01:18.8", "01:21.1", "01:23.4", "01:25.6", "01:27.9",
"01:30.1", "01:32.4", "01:34.7", "01:37.0", "01:39.2", "01:41.5",
"01:43.8", "01:46.0", "01:48.3", "01:50.5", "01:52.8", "01:55.1",
"01:57.3", "01:59.6", "02:01.9", "02:04.1", "02:06.4", "02:08.6",
"02:10.9", "02:13.2", "02:15.4", "02:17.7", "02:19.9", "02:22.2",
"02:24.5", "02:26.7", "02:29.0", "02:31.3", "02:33.5", "02:35.8",
"02:38.0", "02:40.3", "02:42.6", "02:44.8", "02:47.1", "02:49.3",
"02:51.6", "02:53.9", "02:56.2", "02:58.4", "03:00.7", "03:03.0",
"03:05.2", "03:07.5", "03:09.7", "03:12.0"))
I want to count the number of minutes that have passed since counting from the first value
library(lubridate)
q <- ms(tm)
round(as.numeric(q - q[1]) / 60, 2)
But inside the vector a new hour starts and it breaks my code starting from index 292
round(as.numeric(q - q[1]) / 60,2)
[1] 0.00 0.04 0.07 0.11 0.15 0.19 0.22 0.26 0.30 0.34 0.38 0.42
[13] 0.45 0.49 0.53 0.57 0.60 0.64 0.68 0.72 0.75 0.79 0.83 0.87
[25] 0.90 0.94 0.98 1.02 1.05 1.09 1.13 1.17 1.21 1.25 1.28 1.32
[37] 1.36 1.40 1.43 1.47 1.51 1.55 1.58 1.62 1.66 1.70 1.73 1.77
[49] 1.81 1.85 1.89 1.92 1.96 2.00 2.04 2.07 2.11 2.15 2.19 2.22
[61] 2.26 2.30 2.34 2.38 2.41 2.45 2.49 2.53 2.56 2.60 2.64 2.68
[73] 2.72 2.75 2.79 2.83 2.87 2.90 2.94 2.98 3.02 3.06 3.09 3.13
[85] 3.17 3.20 3.24 3.28 3.32 3.36 3.39 3.43 3.47 3.51 3.54 3.58
[97] 3.62 3.66 3.70 3.73 3.77 3.81 3.85 3.88 3.92 3.96 4.00 4.04
[109] 4.07 4.11 4.15 4.19 4.22 4.26 4.30 4.34 4.38 4.41 4.45 4.49
[121] 4.53 4.56 4.60 4.64 4.68 4.72 4.75 4.79 4.83 4.87 4.90 4.94
[133] 4.98 5.02 5.06 5.09 5.13 5.17 5.20 5.24 5.28 5.32 5.36 5.39
[145] 5.43 5.47 5.51 5.54 5.58 5.62 5.66 5.69 5.73 5.77 5.81 5.85
[157] 5.88 5.92 5.96 6.00 6.04 6.07 6.11 6.15 6.19 6.22 6.26 6.30
[169] 6.34 6.38 6.41 6.45 6.49 6.53 6.56 6.60 6.64 6.68 6.71 6.75
[181] 6.79 6.83 6.86 6.90 6.94 6.98 7.02 7.05 7.09 7.13 7.17 7.20
[193] 7.24 7.28 7.32 7.36 7.39 7.43 7.47 7.51 7.54 7.58 7.62 7.66
[205] 7.69 7.73 7.77 7.81 7.84 7.88 7.92 7.96 8.00 8.03 8.07 8.11
[217] 8.15 8.19 8.22 8.26 8.30 8.34 8.37 8.41 8.45 8.49 8.52 8.56
[229] 8.60 8.64 8.68 8.71 8.75 8.79 8.83 8.86 8.90 8.94 8.98 9.01
[241] 9.05 9.09 9.13 9.16 9.20 9.24 9.28 9.32 9.35 9.39 9.43 9.47
[253] 9.50 9.54 9.58 9.62 9.66 9.69 9.73 9.77 9.81 9.85 9.88 9.92
[265] 9.96 10.00 10.03 10.07 10.11 10.15 10.19 10.22 10.26 10.30 10.34 10.37
[277] 10.41 10.45 10.49 10.52 10.56 10.60 10.64 10.68 10.71 10.75 10.79 10.82
[289] 10.86 10.90 10.94 -49.02 -48.99 -48.95 -48.91 -48.87 -48.84 -48.80 -48.76 -48.72
[301] -48.68 -48.65 -48.61 -48.57 -48.53 -48.49 -48.46 -48.42 -48.38 -48.34 -48.31 -48.27
[313] -48.23 -48.19 -48.16 -48.12 -48.08 -48.04 -48.01 -47.97 -47.93 -47.89 -47.86 -47.82
[325] -47.78 -47.74 -47.70 -47.66 -47.63 -47.59 -47.55 -47.52 -47.48 -47.44 -47.40 -47.36
[337] -47.33 -47.29 -47.25 -47.21 -47.17 -47.14 -47.10 -47.06 -47.02 -46.99 -46.95 -46.91
[349] -46.87 -46.84 -46.80 -46.76 -46.72 -46.68 -46.65 -46.61 -46.57 -46.53 -46.50 -46.46
[361] -46.42 -46.38 -46.34 -46.31 -46.27 -46.23 -46.20 -46.16 -46.12 -46.08 -46.04 -46.01
[373] -45.97 -45.93 -45.89 -45.86
How can I get a vector that counts the number of elapsed minutes from the first value, regardless of transitions to a new hour?
Since you've got a vector of times spanning only 60 minutes, you can add 60 minutes to the vector conditional on the time value being less than the starting time.
round(as.numeric(q - q[1]) / 60, 2) + 60 * (q<q[1])
[1] 0.00 0.04 0.07 0.11 0.15 0.19 0.22 0.26 0.30 0.34 0.38
[12] 0.42 0.45 0.49 0.53 0.57 0.60 0.64 0.68 0.72 0.75 0.79
...
[287] 10.79 10.82 10.86 10.90 10.94 10.98 11.01 11.05 11.09 11.13 11.16
[298] 11.20 11.24 11.28 11.32 11.35 11.39 11.43 11.47 11.51 11.54 11.58
[309] 11.62 11.66 11.69 11.73 11.77 11.81 11.84 11.88 11.92 11.96 11.99
[320] 12.03 12.07 12.11 12.14 12.18 12.22 12.26 12.30 12.34 12.37 12.41
[331] 12.45 12.48 12.52 12.56 12.60 12.64 12.67 12.71 12.75 12.79 12.83
[342] 12.86 12.90 12.94 12.98 13.01 13.05 13.09 13.13 13.16 13.20 13.24
[353] 13.28 13.32 13.35 13.39 13.43 13.47 13.50 13.54 13.58 13.62 13.66
[364] 13.69 13.73 13.77 13.80 13.84 13.88 13.92 13.96 13.99 14.03 14.07
[375] 14.11 14.14
We need to surround q<q[1]
by round brackets here ()
to ensure the comparison is executed first (before multiplying by 60), otherwise we'd be adding a logical vector instead of 60 (try 60 * q < q[1]
). Type help(Syntax)
for details on the order of operator syntax and precedence in R.
Also, whenever a logical vector (TRUE/FALSE) meets an arithmetic operation (+, -. *. /), it is coerced into numeric. See: section 2.4 of "An Introduction to R".