strptime() function in R converting time values to 0 - r

I have a column in a data frame (factor type) that I want to convert to POSIXlt using strptime(). What I'm doing is:
file[, 2] <- strptime(file[, 2], format="%H:%M:%S")
Examples of values in the column are: 17:24:00 17:25:00 17:26:00
However, the function returns all 0s. Is the format specified incorrectly? Any help would be appreciated.
Here's the output of dput(head(file[,2])):
structure(1045:1050, .Label = c("00:00:00", "00:01:00", "00:02:00",
"00:03:00", "00:04:00", "00:05:00", "00:06:00", "00:07:00", "00:08:00",
"00:09:00", "00:10:00", "00:11:00", "00:12:00", "00:13:00", "00:14:00",
"00:15:00", "00:16:00", "00:17:00", "00:18:00", "00:19:00", "00:20:00",
"00:21:00", "00:22:00", "00:23:00", "00:24:00", "00:25:00", "00:26:00",
"00:27:00", "00:28:00", "00:29:00", "00:30:00", "00:31:00", "00:32:00",
"00:33:00", "00:34:00", "00:35:00", "00:36:00", "00:37:00", "00:38:00",
"00:39:00", "00:40:00", "00:41:00", "00:42:00", "00:43:00", "00:44:00",
"00:45:00", "00:46:00", "00:47:00", "00:48:00", "00:49:00", "00:50:00",
"00:51:00", "00:52:00", "00:53:00", "00:54:00", "00:55:00", "00:56:00",
"00:57:00", "00:58:00", "00:59:00", "01:00:00", "01:01:00", "01:02:00",
"01:03:00", "01:04:00", "01:05:00", "01:06:00", "01:07:00", "01:08:00",
"01:09:00", "01:10:00", "01:11:00", "01:12:00", "01:13:00", "01:14:00",
"01:15:00", "01:16:00", "01:17:00", "01:18:00", "01:19:00", "01:20:00",
"01:21:00", "01:22:00", "01:23:00", "01:24:00", "01:25:00", "01:26:00",
"01:27:00", "01:28:00", "01:29:00", "01:30:00", "01:31:00", "01:32:00",
"01:33:00", "01:34:00", "01:35:00", "01:36:00", "01:37:00", "01:38:00",
"01:39:00", "01:40:00", "01:41:00", "01:42:00", "01:43:00", "01:44:00",
"01:45:00", "01:46:00", "01:47:00", "01:48:00", "01:49:00", "01:50:00",
"01:51:00", "01:52:00", "01:53:00", "01:54:00", "01:55:00", "01:56:00",
"01:57:00", "01:58:00", "01:59:00", "02:00:00", "02:01:00", "02:02:00",
"02:03:00", "02:04:00", "02:05:00", "02:06:00", "02:07:00", "02:08:00",
"02:09:00", "02:10:00", "02:11:00", "02:12:00", "02:13:00", "02:14:00",
"02:15:00", "02:16:00", "02:17:00", "02:18:00", "02:19:00", "02:20:00",
"02:21:00", "02:22:00", "02:23:00", "02:24:00", "02:25:00", "02:26:00",
"02:27:00", "02:28:00", "02:29:00", "02:30:00", "02:31:00", "02:32:00",
"02:33:00", "02:34:00", "02:35:00", "02:36:00", "02:37:00", "02:38:00",
"02:39:00", "02:40:00", "02:41:00", "02:42:00", "02:43:00", "02:44:00",
"02:45:00", "02:46:00", "02:47:00", "02:48:00", "02:49:00", "02:50:00",
"02:51:00", "02:52:00", "02:53:00", "02:54:00", "02:55:00", "02:56:00",
"02:57:00", "02:58:00", "02:59:00", "03:00:00", "03:01:00", "03:02:00",
"03:03:00", "03:04:00", "03:05:00", "03:06:00", "03:07:00", "03:08:00",
"03:09:00", "03:10:00", "03:11:00", "03:12:00", "03:13:00", "03:14:00",
"03:15:00", "03:16:00", "03:17:00", "03:18:00", "03:19:00", "03:20:00",
"03:21:00", "03:22:00", "03:23:00", "03:24:00", "03:25:00", "03:26:00",
"03:27:00", "03:28:00", "03:29:00", "03:30:00", "03:31:00", "03:32:00",
"03:33:00", "03:34:00", "03:35:00", "03:36:00", "03:37:00", "03:38:00",
"03:39:00", "03:40:00", "03:41:00", "03:42:00", "03:43:00", "03:44:00",
"03:45:00", "03:46:00", "03:47:00", "03:48:00", "03:49:00", "03:50:00",
"03:51:00", "03:52:00", "03:53:00", "03:54:00", "03:55:00", "03:56:00",
"03:57:00", "03:58:00", "03:59:00", "04:00:00", "04:01:00", "04:02:00",
"04:03:00", "04:04:00", "04:05:00", "04:06:00", "04:07:00", "04:08:00",
"04:09:00", "04:10:00", "04:11:00", "04:12:00", "04:13:00", "04:14:00",
"04:15:00", "04:16:00", "04:17:00", "04:18:00", "04:19:00", "04:20:00",
"04:21:00", "04:22:00", "04:23:00", "04:24:00", "04:25:00", "04:26:00",
"04:27:00", "04:28:00", "04:29:00", "04:30:00", "04:31:00", "04:32:00",
"04:33:00", "04:34:00", "04:35:00", "04:36:00", "04:37:00", "04:38:00",
"04:39:00", "04:40:00", "04:41:00", "04:42:00", "04:43:00", "04:44:00",
"04:45:00", "04:46:00", "04:47:00", "04:48:00", "04:49:00", "04:50:00",
"04:51:00", "04:52:00", "04:53:00", "04:54:00", "04:55:00", "04:56:00",
"04:57:00", "04:58:00", "04:59:00", "05:00:00", "05:01:00", "05:02:00",
"05:03:00", "05:04:00", "05:05:00", "05:06:00", "05:07:00", "05:08:00",
"05:09:00", "05:10:00", "05:11:00", "05:12:00", "05:13:00", "05:14:00",
"05:15:00", "05:16:00", "05:17:00", "05:18:00", "05:19:00", "05:20:00",
"05:21:00", "05:22:00", "05:23:00", "05:24:00", "05:25:00", "05:26:00",
"05:27:00", "05:28:00", "05:29:00", "05:30:00", "05:31:00", "05:32:00",
"05:33:00", "05:34:00", "05:35:00", "05:36:00", "05:37:00", "05:38:00",
"05:39:00", "05:40:00", "05:41:00", "05:42:00", "05:43:00", "05:44:00",
"05:45:00", "05:46:00", "05:47:00", "05:48:00", "05:49:00", "05:50:00",
"05:51:00", "05:52:00", "05:53:00", "05:54:00", "05:55:00", "05:56:00",
"05:57:00", "05:58:00", "05:59:00", "06:00:00", "06:01:00", "06:02:00",
"06:03:00", "06:04:00", "06:05:00", "06:06:00", "06:07:00", "06:08:00",
"06:09:00", "06:10:00", "06:11:00", "06:12:00", "06:13:00", "06:14:00",
"06:15:00", "06:16:00", "06:17:00", "06:18:00", "06:19:00", "06:20:00",
"06:21:00", "06:22:00", "06:23:00", "06:24:00", "06:25:00", "06:26:00",
"06:27:00", "06:28:00", "06:29:00", "06:30:00", "06:31:00", "06:32:00",
"06:33:00", "06:34:00", "06:35:00", "06:36:00", "06:37:00", "06:38:00",
"06:39:00", "06:40:00", "06:41:00", "06:42:00", "06:43:00", "06:44:00",
"06:45:00", "06:46:00", "06:47:00", "06:48:00", "06:49:00", "06:50:00",
"06:51:00", "06:52:00", "06:53:00", "06:54:00", "06:55:00", "06:56:00",
"06:57:00", "06:58:00", "06:59:00", "07:00:00", "07:01:00", "07:02:00",
"07:03:00", "07:04:00", "07:05:00", "07:06:00", "07:07:00", "07:08:00",
"07:09:00", "07:10:00", "07:11:00", "07:12:00", "07:13:00", "07:14:00",
"07:15:00", "07:16:00", "07:17:00", "07:18:00", "07:19:00", "07:20:00",
"07:21:00", "07:22:00", "07:23:00", "07:24:00", "07:25:00", "07:26:00",
"07:27:00", "07:28:00", "07:29:00", "07:30:00", "07:31:00", "07:32:00",
"07:33:00", "07:34:00", "07:35:00", "07:36:00", "07:37:00", "07:38:00",
"07:39:00", "07:40:00", "07:41:00", "07:42:00", "07:43:00", "07:44:00",
"07:45:00", "07:46:00", "07:47:00", "07:48:00", "07:49:00", "07:50:00",
"07:51:00", "07:52:00", "07:53:00", "07:54:00", "07:55:00", "07:56:00",
"07:57:00", "07:58:00", "07:59:00", "08:00:00", "08:01:00", "08:02:00",
"08:03:00", "08:04:00", "08:05:00", "08:06:00", "08:07:00", "08:08:00",
"08:09:00", "08:10:00", "08:11:00", "08:12:00", "08:13:00", "08:14:00",
"08:15:00", "08:16:00", "08:17:00", "08:18:00", "08:19:00", "08:20:00",
"08:21:00", "08:22:00", "08:23:00", "08:24:00", "08:25:00", "08:26:00",
"08:27:00", "08:28:00", "08:29:00", "08:30:00", "08:31:00", "08:32:00",
"08:33:00", "08:34:00", "08:35:00", "08:36:00", "08:37:00", "08:38:00",
"08:39:00", "08:40:00", "08:41:00", "08:42:00", "08:43:00", "08:44:00",
"08:45:00", "08:46:00", "08:47:00", "08:48:00", "08:49:00", "08:50:00",
"08:51:00", "08:52:00", "08:53:00", "08:54:00", "08:55:00", "08:56:00",
"08:57:00", "08:58:00", "08:59:00", "09:00:00", "09:01:00", "09:02:00",
"09:03:00", "09:04:00", "09:05:00", "09:06:00", "09:07:00", "09:08:00",
"09:09:00", "09:10:00", "09:11:00", "09:12:00", "09:13:00", "09:14:00",
"09:15:00", "09:16:00", "09:17:00", "09:18:00", "09:19:00", "09:20:00",
"09:21:00", "09:22:00", "09:23:00", "09:24:00", "09:25:00", "09:26:00",
"09:27:00", "09:28:00", "09:29:00", "09:30:00", "09:31:00", "09:32:00",
"09:33:00", "09:34:00", "09:35:00", "09:36:00", "09:37:00", "09:38:00",
"09:39:00", "09:40:00", "09:41:00", "09:42:00", "09:43:00", "09:44:00",
"09:45:00", "09:46:00", "09:47:00", "09:48:00", "09:49:00", "09:50:00",
"09:51:00", "09:52:00", "09:53:00", "09:54:00", "09:55:00", "09:56:00",
"09:57:00", "09:58:00", "09:59:00", "10:00:00", "10:01:00", "10:02:00",
"10:03:00", "10:04:00", "10:05:00", "10:06:00", "10:07:00", "10:08:00",
"10:09:00", "10:10:00", "10:11:00", "10:12:00", "10:13:00", "10:14:00",
"10:15:00", "10:16:00", "10:17:00", "10:18:00", "10:19:00", "10:20:00",
"10:21:00", "10:22:00", "10:23:00", "10:24:00", "10:25:00", "10:26:00",
"10:27:00", "10:28:00", "10:29:00", "10:30:00", "10:31:00", "10:32:00",
"10:33:00", "10:34:00", "10:35:00", "10:36:00", "10:37:00", "10:38:00",
"10:39:00", "10:40:00", "10:41:00", "10:42:00", "10:43:00", "10:44:00",
"10:45:00", "10:46:00", "10:47:00", "10:48:00", "10:49:00", "10:50:00",
"10:51:00", "10:52:00", "10:53:00", "10:54:00", "10:55:00", "10:56:00",
"10:57:00", "10:58:00", "10:59:00", "11:00:00", "11:01:00", "11:02:00",
"11:03:00", "11:04:00", "11:05:00", "11:06:00", "11:07:00", "11:08:00",
"11:09:00", "11:10:00", "11:11:00", "11:12:00", "11:13:00", "11:14:00",
"11:15:00", "11:16:00", "11:17:00", "11:18:00", "11:19:00", "11:20:00",
"11:21:00", "11:22:00", "11:23:00", "11:24:00", "11:25:00", "11:26:00",
"11:27:00", "11:28:00", "11:29:00", "11:30:00", "11:31:00", "11:32:00",
"11:33:00", "11:34:00", "11:35:00", "11:36:00", "11:37:00", "11:38:00",
"11:39:00", "11:40:00", "11:41:00", "11:42:00", "11:43:00", "11:44:00",
"11:45:00", "11:46:00", "11:47:00", "11:48:00", "11:49:00", "11:50:00",
"11:51:00", "11:52:00", "11:53:00", "11:54:00", "11:55:00", "11:56:00",
"11:57:00", "11:58:00", "11:59:00", "12:00:00", "12:01:00", "12:02:00",
"12:03:00", "12:04:00", "12:05:00", "12:06:00", "12:07:00", "12:08:00",
"12:09:00", "12:10:00", "12:11:00", "12:12:00", "12:13:00", "12:14:00",
"12:15:00", "12:16:00", "12:17:00", "12:18:00", "12:19:00", "12:20:00",
"12:21:00", "12:22:00", "12:23:00", "12:24:00", "12:25:00", "12:26:00",
"12:27:00", "12:28:00", "12:29:00", "12:30:00", "12:31:00", "12:32:00",
"12:33:00", "12:34:00", "12:35:00", "12:36:00", "12:37:00", "12:38:00",
"12:39:00", "12:40:00", "12:41:00", "12:42:00", "12:43:00", "12:44:00",
"12:45:00", "12:46:00", "12:47:00", "12:48:00", "12:49:00", "12:50:00",
"12:51:00", "12:52:00", "12:53:00", "12:54:00", "12:55:00", "12:56:00",
"12:57:00", "12:58:00", "12:59:00", "13:00:00", "13:01:00", "13:02:00",
"13:03:00", "13:04:00", "13:05:00", "13:06:00", "13:07:00", "13:08:00",
"13:09:00", "13:10:00", "13:11:00", "13:12:00", "13:13:00", "13:14:00",
"13:15:00", "13:16:00", "13:17:00", "13:18:00", "13:19:00", "13:20:00",
"13:21:00", "13:22:00", "13:23:00", "13:24:00", "13:25:00", "13:26:00",
"13:27:00", "13:28:00", "13:29:00", "13:30:00", "13:31:00", "13:32:00",
"13:33:00", "13:34:00", "13:35:00", "13:36:00", "13:37:00", "13:38:00",
"13:39:00", "13:40:00", "13:41:00", "13:42:00", "13:43:00", "13:44:00",
"13:45:00", "13:46:00", "13:47:00", "13:48:00", "13:49:00", "13:50:00",
"13:51:00", "13:52:00", "13:53:00", "13:54:00", "13:55:00", "13:56:00",
"13:57:00", "13:58:00", "13:59:00", "14:00:00", "14:01:00", "14:02:00",
"14:03:00", "14:04:00", "14:05:00", "14:06:00", "14:07:00", "14:08:00",
"14:09:00", "14:10:00", "14:11:00", "14:12:00", "14:13:00", "14:14:00",
"14:15:00", "14:16:00", "14:17:00", "14:18:00", "14:19:00", "14:20:00",
"14:21:00", "14:22:00", "14:23:00", "14:24:00", "14:25:00", "14:26:00",
"14:27:00", "14:28:00", "14:29:00", "14:30:00", "14:31:00", "14:32:00",
"14:33:00", "14:34:00", "14:35:00", "14:36:00", "14:37:00", "14:38:00",
"14:39:00", "14:40:00", "14:41:00", "14:42:00", "14:43:00", "14:44:00",
"14:45:00", "14:46:00", "14:47:00", "14:48:00", "14:49:00", "14:50:00",
"14:51:00", "14:52:00", "14:53:00", "14:54:00", "14:55:00", "14:56:00",
"14:57:00", "14:58:00", "14:59:00", "15:00:00", "15:01:00", "15:02:00",
"15:03:00", "15:04:00", "15:05:00", "15:06:00", "15:07:00", "15:08:00",
"15:09:00", "15:10:00", "15:11:00", "15:12:00", "15:13:00", "15:14:00",
"15:15:00", "15:16:00", "15:17:00", "15:18:00", "15:19:00", "15:20:00",
"15:21:00", "15:22:00", "15:23:00", "15:24:00", "15:25:00", "15:26:00",
"15:27:00", "15:28:00", "15:29:00", "15:30:00", "15:31:00", "15:32:00",
"15:33:00", "15:34:00", "15:35:00", "15:36:00", "15:37:00", "15:38:00",
"15:39:00", "15:40:00", "15:41:00", "15:42:00", "15:43:00", "15:44:00",
"15:45:00", "15:46:00", "15:47:00", "15:48:00", "15:49:00", "15:50:00",
"15:51:00", "15:52:00", "15:53:00", "15:54:00", "15:55:00", "15:56:00",
"15:57:00", "15:58:00", "15:59:00", "16:00:00", "16:01:00", "16:02:00",
"16:03:00", "16:04:00", "16:05:00", "16:06:00", "16:07:00", "16:08:00",
"16:09:00", "16:10:00", "16:11:00", "16:12:00", "16:13:00", "16:14:00",
"16:15:00", "16:16:00", "16:17:00", "16:18:00", "16:19:00", "16:20:00",
"16:21:00", "16:22:00", "16:23:00", "16:24:00", "16:25:00", "16:26:00",
"16:27:00", "16:28:00", "16:29:00", "16:30:00", "16:31:00", "16:32:00",
"16:33:00", "16:34:00", "16:35:00", "16:36:00", "16:37:00", "16:38:00",
"16:39:00", "16:40:00", "16:41:00", "16:42:00", "16:43:00", "16:44:00",
"16:45:00", "16:46:00", "16:47:00", "16:48:00", "16:49:00", "16:50:00",
"16:51:00", "16:52:00", "16:53:00", "16:54:00", "16:55:00", "16:56:00",
"16:57:00", "16:58:00", "16:59:00", "17:00:00", "17:01:00", "17:02:00",
"17:03:00", "17:04:00", "17:05:00", "17:06:00", "17:07:00", "17:08:00",
"17:09:00", "17:10:00", "17:11:00", "17:12:00", "17:13:00", "17:14:00",
"17:15:00", "17:16:00", "17:17:00", "17:18:00", "17:19:00", "17:20:00",
"17:21:00", "17:22:00", "17:23:00", "17:24:00", "17:25:00", "17:26:00",
"17:27:00", "17:28:00", "17:29:00", "17:30:00", "17:31:00", "17:32:00",
"17:33:00", "17:34:00", "17:35:00", "17:36:00", "17:37:00", "17:38:00",
"17:39:00", "17:40:00", "17:41:00", "17:42:00", "17:43:00", "17:44:00",
"17:45:00", "17:46:00", "17:47:00", "17:48:00", "17:49:00", "17:50:00",
"17:51:00", "17:52:00", "17:53:00", "17:54:00", "17:55:00", "17:56:00",
"17:57:00", "17:58:00", "17:59:00", "18:00:00", "18:01:00", "18:02:00",
"18:03:00", "18:04:00", "18:05:00", "18:06:00", "18:07:00", "18:08:00",
"18:09:00", "18:10:00", "18:11:00", "18:12:00", "18:13:00", "18:14:00",
"18:15:00", "18:16:00", "18:17:00", "18:18:00", "18:19:00", "18:20:00",
"18:21:00", "18:22:00", "18:23:00", "18:24:00", "18:25:00", "18:26:00",
"18:27:00", "18:28:00", "18:29:00", "18:30:00", "18:31:00", "18:32:00",
"18:33:00", "18:34:00", "18:35:00", "18:36:00", "18:37:00", "18:38:00",
"18:39:00", "18:40:00", "18:41:00", "18:42:00", "18:43:00", "18:44:00",
"18:45:00", "18:46:00", "18:47:00", "18:48:00", "18:49:00", "18:50:00",
"18:51:00", "18:52:00", "18:53:00", "18:54:00", "18:55:00", "18:56:00",
"18:57:00", "18:58:00", "18:59:00", "19:00:00", "19:01:00", "19:02:00",
"19:03:00", "19:04:00", "19:05:00", "19:06:00", "19:07:00", "19:08:00",
"19:09:00", "19:10:00", "19:11:00", "19:12:00", "19:13:00", "19:14:00",
"19:15:00", "19:16:00", "19:17:00", "19:18:00", "19:19:00", "19:20:00",
"19:21:00", "19:22:00", "19:23:00", "19:24:00", "19:25:00", "19:26:00",
"19:27:00", "19:28:00", "19:29:00", "19:30:00", "19:31:00", "19:32:00",
"19:33:00", "19:34:00", "19:35:00", "19:36:00", "19:37:00", "19:38:00",
"19:39:00", "19:40:00", "19:41:00", "19:42:00", "19:43:00", "19:44:00",
"19:45:00", "19:46:00", "19:47:00", "19:48:00", "19:49:00", "19:50:00",
"19:51:00", "19:52:00", "19:53:00", "19:54:00", "19:55:00", "19:56:00",
"19:57:00", "19:58:00", "19:59:00", "20:00:00", "20:01:00", "20:02:00",
"20:03:00", "20:04:00", "20:05:00", "20:06:00", "20:07:00", "20:08:00",
"20:09:00", "20:10:00", "20:11:00", "20:12:00", "20:13:00", "20:14:00",
"20:15:00", "20:16:00", "20:17:00", "20:18:00", "20:19:00", "20:20:00",
"20:21:00", "20:22:00", "20:23:00", "20:24:00", "20:25:00", "20:26:00",
"20:27:00", "20:28:00", "20:29:00", "20:30:00", "20:31:00", "20:32:00",
"20:33:00", "20:34:00", "20:35:00", "20:36:00", "20:37:00", "20:38:00",
"20:39:00", "20:40:00", "20:41:00", "20:42:00", "20:43:00", "20:44:00",
"20:45:00", "20:46:00", "20:47:00", "20:48:00", "20:49:00", "20:50:00",
"20:51:00", "20:52:00", "20:53:00", "20:54:00", "20:55:00", "20:56:00",
"20:57:00", "20:58:00", "20:59:00", "21:00:00", "21:01:00", "21:02:00",
"21:03:00", "21:04:00", "21:05:00", "21:06:00", "21:07:00", "21:08:00",
"21:09:00", "21:10:00", "21:11:00", "21:12:00", "21:13:00", "21:14:00",
"21:15:00", "21:16:00", "21:17:00", "21:18:00", "21:19:00", "21:20:00",
"21:21:00", "21:22:00", "21:23:00", "21:24:00", "21:25:00", "21:26:00",
"21:27:00", "21:28:00", "21:29:00", "21:30:00", "21:31:00", "21:32:00",
"21:33:00", "21:34:00", "21:35:00", "21:36:00", "21:37:00", "21:38:00",
"21:39:00", "21:40:00", "21:41:00", "21:42:00", "21:43:00", "21:44:00",
"21:45:00", "21:46:00", "21:47:00", "21:48:00", "21:49:00", "21:50:00",
"21:51:00", "21:52:00", "21:53:00", "21:54:00", "21:55:00", "21:56:00",
"21:57:00", "21:58:00", "21:59:00", "22:00:00", "22:01:00", "22:02:00",
"22:03:00", "22:04:00", "22:05:00", "22:06:00", "22:07:00", "22:08:00",
"22:09:00", "22:10:00", "22:11:00", "22:12:00", "22:13:00", "22:14:00",
"22:15:00", "22:16:00", "22:17:00", "22:18:00", "22:19:00", "22:20:00",
"22:21:00", "22:22:00", "22:23:00", "22:24:00", "22:25:00", "22:26:00",
"22:27:00", "22:28:00", "22:29:00", "22:30:00", "22:31:00", "22:32:00",
"22:33:00", "22:34:00", "22:35:00", "22:36:00", "22:37:00", "22:38:00",
"22:39:00", "22:40:00", "22:41:00", "22:42:00", "22:43:00", "22:44:00",
"22:45:00", "22:46:00", "22:47:00", "22:48:00", "22:49:00", "22:50:00",
"22:51:00", "22:52:00", "22:53:00", "22:54:00", "22:55:00", "22:56:00",
"22:57:00", "22:58:00", "22:59:00", "23:00:00", "23:01:00", "23:02:00",
"23:03:00", "23:04:00", "23:05:00", "23:06:00", "23:07:00", "23:08:00",
"23:09:00", "23:10:00", "23:11:00", "23:12:00", "23:13:00", "23:14:00",
"23:15:00", "23:16:00", "23:17:00", "23:18:00", "23:19:00", "23:20:00",
"23:21:00", "23:22:00", "23:23:00", "23:24:00", "23:25:00", "23:26:00",
"23:27:00", "23:28:00", "23:29:00", "23:30:00", "23:31:00", "23:32:00",
"23:33:00", "23:34:00", "23:35:00", "23:36:00", "23:37:00", "23:38:00",
"23:39:00", "23:40:00", "23:41:00", "23:42:00", "23:43:00", "23:44:00",
"23:45:00", "23:46:00", "23:47:00", "23:48:00", "23:49:00", "23:50:00",
"23:51:00", "23:52:00", "23:53:00", "23:54:00", "23:55:00", "23:56:00",
"23:57:00", "23:58:00", "23:59:00"), class = "factor")

Use [[ instead of [ and it will work:
file[[2]] <- strptime(file[[2]], "%X")
The result:
[1] "2014-06-04 17:24:00" "2014-06-04 17:25:00"
[3] "2014-06-04 17:26:00" "2014-06-04 17:27:00"
[5] "2014-06-04 17:28:00" "2014-06-04 17:29:00"
By the way, "%X" is a short alternative for "%H:%M:%S".

Related

group by year in yearqrt format R

I would like to group values of all other columns by the year in column yearqtr the following data
dput(narepurchasement)
structure(list(Date = structure(c(844128000, 852076800, 859852800,
867715200, 875664000, 883612800, 891388800, 899251200, 907200000,
915148800, 922924800, 930787200, 938736000, 946684800, 954547200,
962409600, 970358400, 978307200, 986083200, 993945600, 1001894400,
1009843200, 1017619200, 1025481600, 1033430400, 1041379200, 1049155200,
1057017600, 1064966400, 1072915200, 1080777600, 1088640000, 1096588800,
1104537600, 1112313600, 1120176000, 1128124800, 1136073600, 1143849600,
1151712000, 1159660800, 1167609600, 1175385600, 1183248000, 1191196800,
1199145600, 1207008000, 1214870400, 1222819200, 1230768000, 1238544000,
1246406400, 1254355200, 1262304000, 1270080000, 1277942400, 1285891200,
1293840000, 1301616000, 1309478400, 1317427200, 1325376000, 1333238400,
1341100800, 1349049600, 1356998400, 1364774400, 1372636800, 1380585600,
1388534400, 1396310400, 1404172800, 1412121600, 1420070400, 1427846400,
1435708800, 1443657600, 1451606400, 1459468800, 1467331200, 1475280000,
1483228800, 1491004800, 1498867200, 1506816000, 1514764800, 1522540800,
1530403200, 1538352000, 1546300800, 1554076800, 1561939200, 1569888000,
1577836800, 1585699200, 1593561600, 1601510400, 1609459200, 1617235200,
1625097600, 1633046400, 1640995200, 1648771200), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), NetIssuance = c("-7450", "-13950",
"-14675", "-22875", "-25875", "-21675", "-17808", "-64840", "-111214",
"-6920", "-76700", "-26188", "-1", "27044", "-50630", "-10731",
"-83887", "-4850", "-14775", "-27350", "-1150", "-2644", "6357",
"-20316", "2098", "-10173", "-3438", "0", "-2055", "-0.802",
"-16823", "-32200", "-70730", "-43031", "-58722", "-90630", "-83784",
"-110795", "-116977", "-107859", "-137542", "-109583", "-149516",
"-162019", "-226618", "-84099", "-38612", "-73533", "-93475",
"-37950", "39311", "20920", "-62302", "-35987", "-35433", "-71238",
"-58295", "-59766", "-101392", "-133088", "-88329", "-49568",
"-99135", "-73428", "-77876", "-38256", "-73497", "-60269", "-105274",
"-101911", "-48493", "-80452", "-71090", "-116963", "-102404",
"-129399", "-104711", "-127487", "-136914", "-150658", "-80792",
"-89438", "-55464", "-119607", "-61042", "-122438", "-225035",
"-79778", "-190075", "-174006", "-46583", "-111504", "-124927",
"-95947", "-14946", "7398", "-67450", "-30403", "-133211", "-218291",
"-237670", "-227868", "-135084"), GrossIssuance = c(35393, 34426,
39963, 36586, 40630, 36993, 57637, 31110, 52737, 52487, 78711,
65846, 95574, 113349, 86067, 75480, 71906, 54552, 64094, 39824,
55322, 43624, 50257, 29329, 35664, 32098, 36084, 42285, 48634,
57955, 47497, 43892, 55599, 48385, 52197, 63692, 63159, 68401,
69557, 63825, 94723, 88627, 97967, 102944, 108022, 86316, 96002,
93730, 75885, 64674, 77307, 62616, 66705, 54873, 57173, 48392,
68703, 64334, 69966, 43637, 55198, 66678, 70380, 68331, 72198,
73702, 83784, 103945, 94138, 89471, 100239, 100418, 111302, 129933,
124281, 116589, 97678, 106734, 118234, 106262, 107965, 122679,
115625, 107485, 112226, 106358, 99560, 101952, 91526, 95447,
118912, 108570, 100615, 107853, 154908, 134115, 154227, 163567,
126579, 112180, 132474, 92327, 80342), GrossRetirement = c(42843,
48376, 54638, 59461, 66505, 58668, 75445, 95950, 163951, 59407,
155411, 92034, 96134, 86305, 136697, 86211, 155793, 59402, 78869,
67174, 56472, 46268, 43900, 49645, 33566, 42271, 39522, 42226,
50689, 58757, 64320, 76092, 126329, 91416, 110919, 154322, 146943,
179196, 186534, 171684, 232265, 198210, 247483, 264963, 334640,
170415, 134614, 167263, 169360, 102624, 37996, 41696, 129007,
90860, 92606, 119630, 126998, 124100, 171358, 176725, 143527,
116246, 169515, 141759, 150074, 111958, 157281, 164214, 199412,
191382, 148732, 180870, 182392, 246896, 226685, 245988, 202389,
234221, 255148, 256920, 188757, 212117, 171089, 227092, 173268,
228796, 324595, 181730, 281601, 269453, 165495, 220074, 225542,
203800, 169854, 126717, 221677, 193970, 259790, 330471, 370144,
320195, 215426), Repurchases = c(22263, 22638, 23514, 25005,
34369, 26643, 29082, 41095, 27253, 31805, 30779, 29350, 35972,
38084, 22859, 24761, 30152, 25245, 26623, 27689, 24038, 20954,
27243, 27314, 18885, 20208, 22000, 25993, 34329, 31567, 34011,
42358, 46643, 52980, 63201, 66599, 90778, 76295, 97243, 91990,
96248, 92541, 121025, 121251, 98213, 94359, 75799, 80943, 45745,
26459, 17862, 24888, 33600, 40277, 59624, 57199, 62624, 66172,
73022, 96186, 74495, 64511, 83483, 65770, 86040, 77135, 100169,
97375, 105120, 124551, 99652, 108215, 106062, 113685, 100343,
122057, 107005, 123418, 99546, 75010, 89025, 93073, 81638, 84879,
87762, 143170, 138764, 134874, 148169, 137193, 107400, 108922,
119371, 143785, 79929, 88312, 110984, 128796, 141252, 154680,
195502, 220050, 120000), MA = c(20579, 25738, 31124, 34456, 32136,
32025, 46364, 54855, 136698, 27602, 124632, 62684, 60162, 48221,
113837, 61450, 125641, 34157, 52246, 39486, 32434, 25314, 16657,
22331, 14681, 22063, 17522, 16233, 16360, 27191, 30309, 33735,
79686, 38436, 47718, 87723, 56166, 102901, 89291, 79694, 136016,
105669, 126458, 143711, 236427, 76055, 58816, 86320, 123615,
76166, 20134, 16809, 95407, 50583, 32982, 62430, 64373, 57928,
98336, 80539, 69032, 51735, 86032, 75988, 64033, 34823, 57112,
66838, 94292, 66831, 49079, 72655, 76330, 133211, 126342, 123931,
95384, 110803, 155602, 181911, 99732, 119044, 89451, 142213,
85506, 85626, 185832, 46856, 133432, 132260, 58095, 111152, 106172,
60015, 89925, 38404, 110693, 65174, 118539, 175791, 174642, 100146,
95426), GDP = c(8259.771, 8362.655, 8518.825, 8662.823, 8765.907,
8866.48, 8969.699, 9121.097, 9293.991, 9411.682, 9526.21, 9686.626,
9900.169, 10002.179, 10247.72, 10318.165, 10435.744, 10470.231,
10599, 10598.02, 10660.465, 10783.5, 10887.46, 10984.04, 11061.433,
11174.129, 11312.766, 11566.669, 11772.234, 11923.447, 12112.815,
12305.307, 12527.214, 12767.286, 12922.656, 13142.642, 13324.204,
13599.16, 13753.424, 13870.188, 14039.56, 14215.651, 14402.082,
14564.117, 14715.058, 14706.538, 14865.701, 14898.999, 14608.208,
14430.901, 14381.236, 14448.882, 14651.248, 14764.611, 14980.193,
15141.605, 15309.471, 15351.444, 15557.535, 15647.681, 15842.267,
16068.824, 16207.13, 16319.54, 16420.386, 16629.05, 16699.551,
16911.068, 17133.114, 17144.281, 17462.703, 17743.227, 17852.54,
17991.348, 18193.707, 18306.96, 18332.079, 18425.306, 18611.617,
18775.459, 18968.041, 19148.194, 19304.506, 19561.896, 19894.75,
20155.486, 20470.197, 20687.278, 20819.269, 21013.085, 21272.448,
21531.839, 21706.532, 21538.032, 19636.731, 21362.428, 21704.706,
22313.85, 23046.934, 23550.42, 24349.121, 24740.48, 25248.476
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-103L))
I don't know how exactly...
We may use
library(dplyr)
library(zoo)
library(lubridate)
narepurchasement %>%
mutate(yearqtr = as.yearqtr(Date)) %>%
group_by(year = year(yearqtr))

Updating dataframe column value by referring to another dataframe

My first data frame (df) contains Entrydate and ExitDate columns. Another dataframe (n1) has all trading dates. I need a new column in first dataframe calculated as number of days as calculated from the second dataframe. How do I call this dayCount function for each row of df. When I try to use mapply, I am unable to pass n1 as a parameter.
dayCount <- function (startDate, endDate, n1) {
return (nrow(subset(n1, Date >= startDate & Date <= endDate)))
}
df<- structure(list(EntryDate = structure(c(11355, 11418, 11436, 11449,
11520, 11523, 11548, 11620, 11768, 11773), class = "Date"), ExitDate = structure(c(11360,
11422, 11438, 11457, 11522, 11526, 11554, 11625, 11772, 11778
), class = "Date")), row.names = c(22L, 65L, 76L, 84L, 135L,
138L, 155L, 204L, 305L, 307L), class = "data.frame")
n1<- structure(c(11354, 11355, 11358, 11359, 11360, 11361, 11362,
11365, 11366, 11367, 11368, 11369, 11372, 11373, 11374, 11375,
11376, 11379, 11380, 11381, 11382, 11383, 11386, 11388, 11389,
11390, 11393, 11394, 11395, 11396, 11397, 11400, 11401, 11402,
11403, 11404, 11407, 11408, 11409, 11410, 11411, 11414, 11415,
11416, 11418, 11421, 11422, 11423, 11424, 11428, 11429, 11430,
11431, 11432, 11435, 11436, 11437, 11438, 11439, 11442, 11444,
11445, 11446, 11449, 11450, 11451, 11452, 11453, 11456, 11457,
11458, 11459, 11460, 11463, 11464, 11465, 11466, 11467, 11470,
11471, 11472, 11473, 11474, 11477, 11478, 11479, 11480, 11481,
11484, 11485, 11486, 11487, 11488, 11491, 11492, 11493, 11494,
11495, 11498, 11499, 11500, 11501, 11502, 11505, 11506, 11507,
11508, 11509, 11512, 11513, 11514, 11515, 11516, 11519, 11520,
11521, 11522, 11523, 11526, 11527, 11528, 11529, 11530, 11533,
11534, 11535, 11536, 11537, 11540, 11541, 11542, 11543, 11544,
11547, 11548, 11550, 11551, 11554, 11555, 11557, 11558, 11561,
11562, 11563, 11564, 11565, 11568, 11569, 11570, 11571, 11572,
11575, 11576, 11577, 11578, 11579, 11582, 11583, 11584, 11585,
11586, 11589, 11590, 11591, 11592, 11593, 11596, 11598, 11599,
11600, 11603, 11604, 11605, 11606, 11607, 11610, 11611, 11612,
11613, 11614, 11617, 11618, 11619, 11620, 11624, 11625, 11626,
11627, 11628, 11631, 11632, 11633, 11634, 11635, 11638, 11639,
11640, 11641, 11645, 11646, 11647, 11648, 11649, 11652, 11653,
11654, 11655, 11659, 11660, 11661, 11662, 11663, 11666, 11667,
11668, 11669, 11670, 11674, 11675, 11676, 11677, 11680, 11682,
11683, 11684, 11687, 11688, 11689, 11690, 11691, 11694, 11695,
11696, 11697, 11698, 11701, 11702, 11703, 11704, 11705, 11708,
11709, 11710, 11711, 11712, 11715, 11716, 11717, 11718, 11719,
11722, 11723, 11724, 11725, 11726, 11729, 11730, 11731, 11732,
11733, 11736, 11737, 11738, 11739, 11740, 11743, 11744, 11745,
11746, 11747, 11750, 11751, 11752, 11753, 11754, 11757, 11758,
11759, 11760, 11761, 11764, 11765, 11766, 11767, 11768, 11772,
11773, 11774, 11778), class = "Date")
You can use %in% to count number of days in n1 between each EntryDate and ExitDate.
df$dayCount <- colSums(mapply(function(x, y) n1 %in% seq(x, y, by = '1 day'),
df$EntryDate, df$ExitDate))
df
# EntryDate ExitDate dayCount
#22 2001-02-02 2001-02-07 4
#65 2001-04-06 2001-04-10 3
#76 2001-04-24 2001-04-26 3
#84 2001-05-07 2001-05-15 7
#135 2001-07-17 2001-07-19 3
#138 2001-07-20 2001-07-23 2
#155 2001-08-14 2001-08-20 4
#204 2001-10-25 2001-10-30 3
#305 2002-03-22 2002-03-26 2
#307 2002-03-27 2002-04-01 3

Substracting time in R

I want to calculate the amount of time someone spends in bed at night.
First column, j1bed (time goes to bed):
dput(head(subscales$j1bed, 20))
c("23:00:00", "01:00:00", "22:00:00", "00:00:00", "00:00:00",
"23:00:00", "03:00:00", "23:00:00", "22:00:00", "23:00:00", "21:00:00",
"23:30:00", "22:00:00", "22:00:00", "20:00:00", "22:00:00", "22:00:00",
"21:00:00", "20:00:00", "23:00:00")
Second column, j3wake (time wakes up):
dput(head(subscales$j3wake, 20))
c("08:00:00", "08:00:00", "05:00:00", "08:00:00", "07:00:00",
"07:00:00", "09:00:00", "09:00:00", "08:00:00", "06:00:00", "03:00:00",
"08:30:00", "07:00:00", "08:00:00", "09:00:00", "07:00:00", "06:00:00",
"07:00:00", "07:00:00", "12:00:00")
My code currently is as follows:
Hours_in_Bed<- as.numeric(difftime(strptime(subscales$j3wake, "%I:%M %p" ),strptime(subscales$j1bed, "%I:%M %p" ),units='hours'))
For some reason, this is coming up with only "NA". Any ideas?
Edit: I've tried the following three suggestions:
1)
result1<-strptime(subscales$j3wake, "%I:%M %p")
result2<-strptime(subscales$j1bed, "%I:%M %p")
Hours_in_Bed<-as.numeric(difftime(result2, result1, units='hours'))
This yields only "NA"s.
2)
subscales$j3wake1=as.POSIXct(paste('1970-1-1',subscales$j3wake))
subscales$j1bed1 =as.POSIXct(paste('1970-1-1',subscales$j1bed))
as.numeric(difftime(subscales$j1bed1,subscales$j3wake1,units='hours'))
This yields "0"s.
3)
all(grepl("[0-9]{2}:[0-9]{2} (AM|PM)", subscales$j3wake))
all(grepl("[0-9]{2}:[0-9]{2} (AM|PM)", subscales$j1bed))
Both yield "False".
Edit 2: that last tip was key! I think I should be using "%H:%M" instead:
Hours_in_Bed<- as.numeric(difftime(strptime(subscales$j3wake, "%H:%M" ),strptime(subscales$j1bed, "%H:%M" ),units='hours'))
If I then do:
Hours_in_Bed[!is.na(Hours_in_Bed) & Hours_in_Bed<0] <- 24 + Hours_in_Bed[!is.na(Hours_in_Bed) & Hours_in_Bed<0]
I should be good to go. Let me know if I'm still off, and thanks for the help!
It's usually easier to use the POSIXct-type even if there isn't any real year/month/day. Example:
subscales$j3wake1 = as.POSIXct(paste('1970-1-1',subscales$j3wake))
subscales$j1bed1 = as.POSIXct(paste('1970-1-1',subscales$j1bed))
as.numeric(difftime(subscales$j1bed1,subscales$j3wake1,units='hours'))

Create 5 minute intervals starting from 00 in R

I want to divide data in 5 minute intervals and compare them with monthly averages and standard deviation to detect any anomalies. I cut the data in 5 minute intervals as follows:
cut(month.data, breaks = "5 min")
dput(monthlydata)
structure(list(time = c("00:00:00", "00:05:00", "00:10:00", "00:15:00", "00:20:00", "00:25:00", "00:30:00", "00:35:00", "00:40:00", "00:45:00", "00:50:00", "00:55:00", "01:00:00", "01:05:00", "01:10:00", "01:15:00", "01:25:00", "01:30:00", "01:35:00", "01:40:00", "01:45:00", "01:55:00", "02:00:00", "02:20:00", "02:25:00", "02:40:00", "02:45:00", "02:50:00", "03:05:00", "03:15:00", "03:25:00", "03:30:00", "03:35:00", "03:40:00", "03:45:00", "03:50:00", "03:55:00", "04:05:00", "04:25:00", "04:30:00", "04:50:00", "04:55:00", "05:05:00", "05:10:00", "05:15:00", "05:20:00", "05:30:00", "05:40:00", "05:50:00", "05:55:00", "06:00:00", "06:05:00", "06:10:00", "06:15:00", "06:20:00", "06:30:00", "06:35:00", "06:40:00", "06:45:00", "06:55:00", "07:00:00", "07:05:00", "07:10:00", "07:15:00", "07:20:00", "07:25:00", "07:30:00", "07:35:00", "07:40:00", "07:45:00", "07:50:00", "07:55:00", "08:00:00", "08:05:00", "08:10:00", "08:15:00", "08:20:00", "08:25:00", "08:30:00", "08:35:00", "08:40:00", "08:45:00", "08:50:00", "08:55:00", "09:00:00", "09:05:00", "09:10:00", "09:15:00", "09:20:00", "09:25:00", "09:30:00", "09:35:00", "09:40:00", "09:45:00", "09:50:00", "09:55:00", "10:00:00", "10:05:00", "10:10:00", "10:15:00", "10:20:00", "10:25:00", "10:30:00", "10:35:00", "10:40:00", "10:45:00", "10:50:00"), avg = c(50.5510560619622, 43.0189344993435, 50.3126451810161, 51.7984139398903, 44.1216815428764, 54.6443574865021, 50.065771120662, 50.9085361977819, 59.7024337563552, 44.6504863019322, 51.3800349930107, 47.2281110758541, 39.1562855847908, 47.3556824171027, 54.4776585774653, 64.9968044851706, 47.878888210121, 44.7561800618865, 45.0695456307952, 56.1759044802863, 50.8227417957758, 52.6309915011542, 62.7342370217067, 54.4257866432874, 54.3226457929837, 40.1938479668371, 54.5403748037875, 51.8463279336394, 53.8173320832895, 49.9889812414321, 50.5827916556644, 53.5500871960216, 37.0209877205586, 55.2612198888207, 55.1863860227875, 58.2740366768661, 50.5885573635039, 48.1051996319848, 47.7634397043728, 46.5863241465071, 54.1810631439095, 53.7906152156406, 52.6409719258956, 54.81269123308, 44.0537724370726, 47.1977242746078, 46.5010741707819, 45.8433966693518, 42.3623605036368, 43.0730189148746, 58.4167050044254, 49.7799961792657, 53.0755779045083, 52.0552481180891, 42.0602921415756, 52.5126950828788, 55.0870481980705, 44.4144434705709, 42.392966543036, 47.1807241560313, 44.6884956183158, 47.2896481418499, 57.8319708553495, 46.6397655826931, 52.4873442246903, 51.6324293101077, 55.0908694414676, 43.8596455462562, 48.8941181950083, 48.7514252330684, 49.310070422837,48.5234755805063, 58.5542276809981, 47.7720993402378,49.9405735614802, 52.8780543357139, 58.2557463154677,52.9242756783794, 55.4983029733778, 44.2299478555713,57.3379257421419, 50.1951095071188, 53.0553561602009, 50.3109504601222, 44.9316581536335, 47.7568227989573, 50.3093864093436, 50.0461546149579, 48.2373271954793, 50.2943295283144, 46.8054846556807, 48.7084493434669, 46.5421233124519, 50.1222951953386, 49.4207933535255, 46.1521667031027, 51.8644029994928, 53.4831581582472, 51.4972139096679, 51.5181187952616, 49.1998856564675, 50.3454476017966, 49.7587298896826, 45.6040364435812, 49.7466159629413, 51.0983492421099, 52.2935123336372), sd =c(9.32931925004817, 11.6047417906884, 9.80771691435559, 10.2308327194904, 9.67431773674866, 10.5323558825585, 9.1920111408028, 10.0745961985324, 9.21246056157269, 7.96228334027313, 9.89384474113651, 12.3284041772698, 13.5489518864705, 9.96803285037014, 8.85300006821126, 10.9832318078379, 13.0909163134817, 9.7261635496657, 10.3208509302825, 8.74704188744148, 9.44853223258545, 7.68266078719723, 8.78660429415339, 10.1981152232186,10.0617639380203, 9.70826609005244, 10.4405793131911, 10.0294741532956, 10.5843898470973, 10.0678114702352, 9.72005734098214, 8.71988580635692, 13.0339431549482, 8.85263899155544, 11.1140174478773, 10.6801908843647, 11.0442113270832, 9.6576794753704, 11.193361191756, 9.05129333547447, 11.1414887813967, 10.5985803957382, 10.2422686622522, 11.2629702278102, 10.4774845098793, 9.82520095973172, 10.6775347630735, 8.97029695502126, 8.28221177072086, 11.0689605695813, 10.4298020842373, 9.57012379689429, 10.1587613403527, 11.2343452027682, 9.09417849538438, 10.3529463918792, 8.89434012398308, 7.86166740352018, 8.67472741747663, 10.7631616313607, 11.4634738459674, 10.3773119423003, 10.1261492697498, 9.51153382612954, 8.8445878796955, 9.99640290007654, 9.33226675473664, 10.2637002156788, 9.44339377944955, 10.5843418581127, 9.00425609052502, 10.2822169680166, 10.5184763916409, 10.3495044419935, 9.99843947958033, 8.67736455800308, 10.1508472078283, 9.3450979185795, 9.84066593055499, 9.95417999414617, 9.2622985333717, 11.4445218170255, 9.72729168049685, 9.36415135782777, 10.2272372991057, 10.4800042675175, 10.0067598340318, 10.6127856103593, 8.91026514253112, 10.3451668931764, 9.86818949901026, 10.736479643411, 8.6424538599602, 9.53874170759392, 10.5484021386586, 10.7130372286846, 10.2509775336419, 10.7454025452377, 9.63372860813097, 9.35982465121709, 9.47486350661184, 10.311507962123, 10.0338294237329, 9.50474631714252, 10.1746281369343, 9.75040171260143, 10.8675383447689)), class = c("tbl_df", "tbl", "data.frame"), .Names = c("time", "avg", "sd"), row.names = c(NA, -107L))
However if my currdata starts later than 00 min, the time intervals are calculated depending on the earliest entry in the data.So if the first entry is at 01:00 min, the intervals are 01:00 to 06:00 and so on. However, I want the intervals to be 00:00 - 05:00 and so on, to be consistent with monthlydata intervals. Is there a way to set the lowest limit on the intervals in the cut statement? or is there any other way to ensure that both series use the same minutes intervals.
The data for one day
dput(currday)
structure(c(1533769260, 1533769320, 1533769320, 1533769380, 1533769380, 1533769380, 1533769440, 1533769500, 1533769680, 1533769740, 1533769740,1533769740, 1533769800, 1533769920, 1533769920, 1533769920,1533769980,1533770100, 1533770100, 1533770220, 1533770280, 1533770340, 1533770400, 1533770460, 1533770460, 1533770460, 1533770520, 1533770520, 1533770580, 1533770580, 1533770640, 1533770880, 1533771060, 1533771060, 1533771120, 1533771300, 1533771300, 1533771300, 1533771360, 1533771360, 1533771540, 1533771660, 1533771660, 1533771960, 1533772260, 1533772260, 1533772260, 1533772440, 1533772440, 1533772560, 1533772620, 1533772620, 1533772800, 1533772980, 1533773160, 1533773400, 1533773400, 1533773460, 1533773760, 1533773880, 1533774480, 1533774540, 1533774900, 1533774900, 1533774960, 1533775080, 1533775140, 1533775320, 1533775380, 1533775500,1533775560, 1533775680, 1533776340, 1533776520, 1533776520,1533776580, 1533777660, 1533777900, 1533777900, 1533778200, 1533779100, 1533779100, 1533779220, 1533779460, 1533779640, 1533780360, 1533780960, 1533781740, 1533782040, 1533782340, 1533782700, 1533782880, 1533783120, 1533783600, 1533784020, 1533784140, 1533785280, 1533785280, 1533785400, 1533785460, 1533786660), class = c("POSIXct", "POSIXt"), tzone = "Europe/London")
I have looked at other questions and found create-a-24-hour-vector-with-5-minutes-time-interval-in-r. It is not quite what I am after. Any help is highly appreciated.
As suggested by #AleBdC, I checked the link: Grouping every n minutes with dplyr and found the comment suggested by #MikeyHarper very useful.
So, I first create five-minute intervals in currday, then separate the date and time into two columns and then converted the time column into factor and use it for grouping and summarizing.
library(lubridate)
x <- ymd_hms(currday, tz="Europe/London")
x<-data.frame(floor_date(x, unit = "5minutes"))
names(x)<-"by5"
x%>%separate(by5, into = c("date", "time"),sep = "\\s" )
x$time<-as.factor(x$time)
I also created five-minute intervals in my monthly data using the same approach and calculated the mean and sd and compared it with the daily data.

Forecast with auto Arima, with long term trend line, the 30 day forecast "jumps"

I'm trying to create a 30 day forecast using auto.arima from the forecast package. I want to capture the long term trend, so I inserted it into the xreg argument.
The data:
dput(data)
structure(list(TKDate = structure(c(15706, 15707, 15708, 15709,
15710, 15711, 15712, 15713, 15714, 15715, 15716, 15717, 15718,
15719, 15720, 15721, 15722, 15723, 15724, 15725, 15726, 15727,
15728, 15729, 15730, 15731, 15732, 15733, 15734, 15735, 15736,
15737, 15738, 15739, 15740, 15741, 15742, 15743, 15744, 15745,
15746, 15747, 15748, 15749, 15750, 15751, 15752, 15753, 15754,
15755, 15756, 15757, 15758, 15759, 15760, 15761, 15762, 15763,
15764, 15765, 15766, 15767, 15768, 15769, 15770, 15771, 15772,
15773, 15774, 15775, 15776, 15777, 15778, 15779, 15780, 15781,
15782, 15783, 15784, 15785, 15786, 15787, 15788, 15789, 15790,
15791, 15792, 15793, 15794, 15795, 15796, 15797, 15798, 15799,
15800, 15801, 15802, 15803, 15804, 15805, 15806, 15807, 15808,
15809, 15810, 15811, 15812, 15813, 15814, 15815, 15816, 15817,
15818, 15819, 15820, 15821, 15822, 15823, 15824, 15825, 15826,
15827, 15828, 15829, 15830, 15831, 15832, 15833, 15834, 15835,
15836, 15837, 15838, 15839, 15840, 15841, 15842, 15843, 15844,
15845, 15846, 15847, 15848, 15849, 15850, 15851, 15852, 15853,
15854, 15855, 15856, 15857, 15858, 15859, 15860, 15861, 15862,
15863, 15864, 15865, 15866, 15867, 15868, 15869, 15870, 15871,
15872, 15873, 15874, 15875, 15876, 15877, 15878, 15879, 15880,
15881, 15882, 15883, 15884, 15885, 15886, 15887, 15888, 15889,
15890, 15891, 15892, 15893, 15894, 15895, 15896, 15897, 15898,
15899, 15900, 15901, 15902, 15903, 15904, 15905, 15906, 15907,
15908, 15909, 15910, 15911, 15912, 15913, 15914, 15915, 15916,
15917, 15918, 15919, 15920, 15921, 15922, 15923, 15924, 15925,
15926, 15927, 15928, 15929, 15930, 15931, 15932, 15933, 15934,
15935, 15936, 15937, 15938, 15939, 15940, 15941, 15942, 15943,
15944, 15945, 15946, 15947, 15948, 15949, 15950, 15951, 15952,
15953, 15954, 15955, 15956, 15957, 15958, 15959, 15960, 15961,
15962, 15963, 15964, 15965, 15966, 15967, 15968, 15969, 15970,
15971, 15972, 15973, 15974, 15975, 15976, 15977, 15978, 15979,
15980, 15981, 15982, 15983, 15984, 15985, 15986, 15987, 15988,
15989, 15990, 15991, 15992, 15993, 15994, 15995, 15996, 15997,
15998, 15999, 16000, 16001, 16002, 16003, 16004, 16005, 16006,
16007, 16008, 16009, 16010, 16011, 16012, 16013, 16014, 16015,
16016, 16017, 16018, 16019, 16020, 16021, 16022, 16023, 16024,
16025, 16026, 16027, 16028, 16029, 16030, 16031, 16032, 16033,
16034, 16035, 16036, 16037, 16038, 16039, 16040, 16041, 16042,
16043, 16044, 16045, 16046, 16047, 16048, 16049, 16050, 16051,
16052, 16053, 16054, 16055, 16056, 16057, 16058, 16059, 16060,
16061, 16062, 16063, 16064, 16065, 16066, 16067, 16068, 16069,
16070, 16071, 16072, 16073, 16074, 16075, 16076, 16077, 16078,
16079, 16080, 16081, 16082, 16083, 16084, 16085, 16086, 16087,
16088, 16089, 16090, 16091, 16092, 16093, 16094, 16095, 16096,
16097, 16098, 16099, 16100, 16101, 16102, 16103, 16104, 16105,
16106, 16107, 16108, 16109, 16110, 16111, 16112, 16113, 16114,
16115, 16116, 16117, 16118), class = "Date"), spend = c(7984.39,
11476.06, 6555.57, 3981.45, 3963.83, 4827.72, 6309.32, 13503.36,
17075.89, 33353.71, 29324.34, 7968.68, 5540.63, 12113.45, 15596.38,
19328.67, 20224.68, 18977.55, 16128.27, 10633.56, 11887.79, 17881.11,
12613.46, 11607.55, 38232.11, 7861.25, 9397.88, 12056.02, 15115.87,
12275.93, 14537.35, 9594.26, 8215.83, 9632.52, 9993.15, 13478.37,
28509.38, 12016.33, 8907.76, 8757.43, 9513.09, 10299.5, 10385.03,
12515.62, 9008.95, 17825.68, 9320.47, 11189.58, 12902.31, 13341.35,
18675.32, 16989.53, 10114.53, 9876.65, 11203.39, 11718.73, 26264.95,
12414.19, 12275.16, 9242.85, 8883.97, 10095.72, 11581.55, 14815.78,
25064.12, 9297.07, 8047.91, 6876.37, 8881.63, 10982.85, 9975.33,
24124.62, 8514.66, 15719.84, 5807.39, 8422.38, 15184.95, 14757.58,
11087.61, 11070.78, 10425.67, 15517.8, 11257.69, 11915.47, 11720.37,
34064.62, 6493.41, 5757.4, 4387.54, 6520.58, 7806.81, 6356.63,
10916.36, 9013.43, 9722.41, 6044.25, 7971.7, 23933.54, 8627.85,
9722.77, 18660.13, 13011.36, 11445.11, 14219.2, 17138.92, 16016.68,
11434, 31379.03, 8494.25, 12493.85, 7708.1, 21583.05, 9026.17,
9379.35, 8287.13, 7298.16, 6097.03, 8076.57, 12871.87, 11346.89,
9115.82, 7737.98, 15065.38, 5262.73, 6522.58, 12743.94, 23945.16,
16109.26, 6985.89, 6345.08, 6246.93, 6824.66, 8491.42, 9654.99,
18976.58, 19565.68, 8075.47, 7219.79, 8629.04, 12491.64, 11915.89,
27533.16, 13554.35, 10102.21, 20029.15, 11641.82, 15855.19, 14139.17,
15376.63, 14625.99, 9098.87, 9396.64, 12015.84, 17532.75, 15131.65,
15815.5, 16048.65, 9769.63, 9582.12, 11201.8, 12810, 18857.38,
11822.71, 19289.08, 8911.29, 9437.55, 10987.14, 12995.65, 16675.26,
9741.82, 9723.57, 10328.24, 7738.04, 8432.16, 23021.73, 10367.28,
8210.53, 10468.4, 8024.25, 7296.25, 7445.34, 8539.59, 12386.23,
15335.72, 9013.49, 7994.95, 7759.46, 8789.38, 11242.38, 28653.23,
9750.96, 14398.62, 9248.74, 6766.08, 8159.14, 9899.38, 9453.35,
17588.96, 8958.16, 8256.61, 6240.4, 7235.24, 23841.62, 9002.73,
11839.47, 8693.31, 7161.37, 7046.39, 9221.53, 10004.93, 8698.76,
7948.68, 9013.27, 18536.68, 7980.38, 8968.95, 23594.14, 17744.66,
12615.73, 13646.05, 10512.58, 9066.02, 9665.15, 13183.2, 23864.45,
12017.52, 10831.07, 8954.76, 7276.41, 7882.9, 16616.41, 15384.68,
11046.53, 10621.01, 8094.74, 5451.26, 6237.79, 10717.69, 7076.38,
7044.62, 7047.45, 7774.77, 6496.21, 6340.9, 7110.53, 7691.28,
17482.02, 5576.19, 3763.79, 11477.68, 5710.5, 6519.51, 20022.61,
13153.68, 6526.28, 5885.28, 5656.17, 6270.04, 9795.38, 6320.95,
5741.98, 10808.72, 5150.87, 5416.52, 6305.05, 20953.12, 6569.02,
6360.21, 9376.68, 4973.93, 5034.48, 6380.45, 15307.28, 14386.65,
17705.88, 4779.52, 4784.79, 4737.05, 5350.28, 12112.11, 13153.72,
6049.69, 5430.46, 4627.59, 3637.2, 5482.43, 16705.15, 12221.16,
13198.88, 6484.54, 5590.86, 4979.09, 5771.75, 7311.92, 16111.86,
8047.77, 11706.91, 6042.14, 5670.74, 6905.07, 11261.89, 9700.4,
6643.03, 5693.85, 14778.67, 9128.14, 3682.01, 7911.5, 17742.85,
5093.31, 7867.97, 3202.78, 2843.35, 2598.77, 10930.81, 11204.67,
7289.62, 4000.17, 4178.89, 4507.33, 6671.48, 10317.48, 9368.98,
6156.41, 8375.24, 2762.76, 2457.59, 4707.51, 4584.52, 3749.82,
11667.82, 4271.67, 3614.3, 3715.83, 4510.57, 4872.36, 21805.71,
4757.04, 6515.92, 2834.25, 2685.19, 3509.28, 4479.35, 17817.99,
10357.67, 3412.15, 3044.95, 2840.24, 3348.91, 13671.68, 2027.42,
1616.25, 1177.73, 995.25, 1062.25, 1578.07, 1649.8, 1410.06,
1592.03, 3995.24, 6489.87, 6895.21, 8298.58, 7698.68, 5782.07,
7671.08, 19539.4, 7023.84, 6509.9, 6643.28, 19850.3, 6856.67,
13142.15, 5524.75, 5063.2, 4916.81, 6117.54, 6717.86, 9393.95,
10462.44, 10511.15, 4497.94, 4038.31, 5503.91, 5554.82, 5801.11,
12992.82, 4778.61, 4067.41, 4359.53, 6148.1, 9236.51, 5773.16,
11313.13, 4702.37, 4167.3, 4067.75, 4469.11, 9278.41, 9911.18,
5161.13, 4477.78, 4459.53, 4080.14, 5084.67, 7735.34, 10676.6,
5507.86, 8286.12, 4332.23, 4737.52, 5952.09, 7134.44)), .Names = c("TKDate",
"spend"), row.names = c(NA, 413L), class = "data.frame")
The code:
library(forecast)
explaining<-rep(1:length(data$TKDate))
predic<-rep((length(data$TKDate)+1):(length(data$TKDate)+31))
modArima <- auto.arima(data[,2],xreg=explaining)
fit<-forecast(modArima,h=30,xreg=explaining,newdata=predic)
plot(fit)
I get this weird jump:
Can anyone explain to me this weird jump? Why doesn't the forecast continue from the last observed data point (or at least close to it)?
This is a hard to find error, I'll admit.
forecast.Arima() takes the new values of the external regressors not in a newdata parameter (as does predict.lm()), but in the xreg parameter. So instead of
fit <- forecast(modArima,h=30,xreg=explaining,newdata=predic)
where you forecasted using the values of explaining, not those of predic (unfortunately, forecast.Arima() does not throw a warning if you feed data to the nonexistent newdata parameter), do this:
fit <- forecast(modArima,h=30,xreg=predic)
and plot (with in-sample fits thrown in for good measure - EDIT: somewhat confusingly, the in-sample fits are not returned by auto.arima() or arima() as they are by lm(), but by forecast.Arima()):
plot(fit)
lines(fit$fitted,col="red")

Resources