How to create a sequence of time in minutes with date in R?

R ProgrammingServer Side ProgrammingProgramming

To create a sequence of time in minutes with date we can use seq function and define the date and time with as.POSIXct. For example, if we want to generate a sequence of time between 2 pm on tenth October 2020 to 4 pm on the same date then we can use the following code −

seq(from=as.POSIXct("2020-10-10 14:00"),to=as.POSIXct("2020-10-10 16:00"),by="min")

Example

 Live Demo

seq(from=as.POSIXct("2020-01-01 00:01"),to=as.POSIXct("2020-01-01 01:00"),by="min")

Output

[1] "2020-01-01 00:01:00 IST" "2020-01-01 00:02:00 IST"
[3] "2020-01-01 00:03:00 IST" "2020-01-01 00:04:00 IST"
[5] "2020-01-01 00:05:00 IST" "2020-01-01 00:06:00 IST"
[7] "2020-01-01 00:07:00 IST" "2020-01-01 00:08:00 IST"
[9] "2020-01-01 00:09:00 IST" "2020-01-01 00:10:00 IST"
[11] "2020-01-01 00:11:00 IST" "2020-01-01 00:12:00 IST"
[13] "2020-01-01 00:13:00 IST" "2020-01-01 00:14:00 IST"
[15] "2020-01-01 00:15:00 IST" "2020-01-01 00:16:00 IST"
[17] "2020-01-01 00:17:00 IST" "2020-01-01 00:18:00 IST"
[19] "2020-01-01 00:19:00 IST" "2020-01-01 00:20:00 IST"
[21] "2020-01-01 00:21:00 IST" "2020-01-01 00:22:00 IST"
[23] "2020-01-01 00:23:00 IST" "2020-01-01 00:24:00 IST"
[25] "2020-01-01 00:25:00 IST" "2020-01-01 00:26:00 IST"
[27] "2020-01-01 00:27:00 IST" "2020-01-01 00:28:00 IST"
[29] "2020-01-01 00:29:00 IST" "2020-01-01 00:30:00 IST"
[31] "2020-01-01 00:31:00 IST" "2020-01-01 00:32:00 IST"
[33] "2020-01-01 00:33:00 IST" "2020-01-01 00:34:00 IST"
[35] "2020-01-01 00:35:00 IST" "2020-01-01 00:36:00 IST"
[37] "2020-01-01 00:37:00 IST" "2020-01-01 00:38:00 IST"
[39] "2020-01-01 00:39:00 IST" "2020-01-01 00:40:00 IST"
[41] "2020-01-01 00:41:00 IST" "2020-01-01 00:42:00 IST"
[43] "2020-01-01 00:43:00 IST" "2020-01-01 00:44:00 IST"
[45] "2020-01-01 00:45:00 IST" "2020-01-01 00:46:00 IST"
[47] "2020-01-01 00:47:00 IST" "2020-01-01 00:48:00 IST"
[49] "2020-01-01 00:49:00 IST" "2020-01-01 00:50:00 IST"
[51] "2020-01-01 00:51:00 IST" "2020-01-01 00:52:00 IST"
[53] "2020-01-01 00:53:00 IST" "2020-01-01 00:54:00 IST"
[55] "2020-01-01 00:55:00 IST" "2020-01-01 00:56:00 IST"
[57] "2020-01-01 00:57:00 IST" "2020-01-01 00:58:00 IST"
[59] "2020-01-01 00:59:00 IST" "2020-01-01 01:00:00 IST"

Example

 Live Demo

seq(from=as.POSIXct("2020-06-05 00:10"),to=as.POSIXct("2020-06-05 00:50"),by="min")

Output

[1] "2020-06-05 00:10:00 IST" "2020-06-05 00:11:00 IST"
[3] "2020-06-05 00:12:00 IST" "2020-06-05 00:13:00 IST"
[5] "2020-06-05 00:14:00 IST" "2020-06-05 00:15:00 IST"
[7] "2020-06-05 00:16:00 IST" "2020-06-05 00:17:00 IST"
[9] "2020-06-05 00:18:00 IST" "2020-06-05 00:19:00 IST"
[11] "2020-06-05 00:20:00 IST" "2020-06-05 00:21:00 IST"
[13] "2020-06-05 00:22:00 IST" "2020-06-05 00:23:00 IST"
[15] "2020-06-05 00:24:00 IST" "2020-06-05 00:25:00 IST"
[17] "2020-06-05 00:26:00 IST" "2020-06-05 00:27:00 IST"
[19] "2020-06-05 00:28:00 IST" "2020-06-05 00:29:00 IST"
[21] "2020-06-05 00:30:00 IST" "2020-06-05 00:31:00 IST"
[23] "2020-06-05 00:32:00 IST" "2020-06-05 00:33:00 IST"
[25] "2020-06-05 00:34:00 IST" "2020-06-05 00:35:00 IST"
[27] "2020-06-05 00:36:00 IST" "2020-06-05 00:37:00 IST"
[29] "2020-06-05 00:38:00 IST" "2020-06-05 00:39:00 IST"
[31] "2020-06-05 00:40:00 IST" "2020-06-05 00:41:00 IST"
[33] "2020-06-05 00:42:00 IST" "2020-06-05 00:43:00 IST"
[35] "2020-06-05 00:44:00 IST" "2020-06-05 00:45:00 IST"
[37] "2020-06-05 00:46:00 IST" "2020-06-05 00:47:00 IST"
[39] "2020-06-05 00:48:00 IST" "2020-06-05 00:49:00 IST"
[41] "2020-06-05 00:50:00 IST"

Example

We can create these sequences by using different time zones with the help of tz argument as shown below −

 Live Demo

> seq(from=as.POSIXct("2020-06-05 00:10",tz="UTC"),to=as.POSIXct("2020-06-05 00:50",tz="UTC"),by="min")

Output

[1] "2020-06-05 00:10:00 UTC" "2020-06-05 00:11:00 UTC"
[3] "2020-06-05 00:12:00 UTC" "2020-06-05 00:13:00 UTC"
[5] "2020-06-05 00:14:00 UTC" "2020-06-05 00:15:00 UTC"
[7] "2020-06-05 00:16:00 UTC" "2020-06-05 00:17:00 UTC"
[9] "2020-06-05 00:18:00 UTC" "2020-06-05 00:19:00 UTC"
[11] "2020-06-05 00:20:00 UTC" "2020-06-05 00:21:00 UTC"
[13] "2020-06-05 00:22:00 UTC" "2020-06-05 00:23:00 UTC"
[15] "2020-06-05 00:24:00 UTC" "2020-06-05 00:25:00 UTC"
[17] "2020-06-05 00:26:00 UTC" "2020-06-05 00:27:00 UTC"
[19] "2020-06-05 00:28:00 UTC" "2020-06-05 00:29:00 UTC"
[21] "2020-06-05 00:30:00 UTC" "2020-06-05 00:31:00 UTC"
[23] "2020-06-05 00:32:00 UTC" "2020-06-05 00:33:00 UTC"
[25] "2020-06-05 00:34:00 UTC" "2020-06-05 00:35:00 UTC"
[27] "2020-06-05 00:36:00 UTC" "2020-06-05 00:37:00 UTC"
[29] "2020-06-05 00:38:00 UTC" "2020-06-05 00:39:00 UTC"
[31] "2020-06-05 00:40:00 UTC" "2020-06-05 00:41:00 UTC"
[33] "2020-06-05 00:42:00 UTC" "2020-06-05 00:43:00 UTC"
[35] "2020-06-05 00:44:00 UTC" "2020-06-05 00:45:00 UTC"
[37] "2020-06-05 00:46:00 UTC" "2020-06-05 00:47:00 UTC"
[39] "2020-06-05 00:48:00 UTC" "2020-06-05 00:49:00 UTC"
[41] "2020-06-05 00:50:00 UTC"

Example

 Live Demo

> seq(from=as.POSIXct("2020-06-05 00:10",tz="GMT"),to=as.POSIXct("2020-06-05 00:50",tz="GMT"),by="min")

Output

[1] "2020-06-05 00:10:00 GMT" "2020-06-05 00:11:00 GMT"
[3] "2020-06-05 00:12:00 GMT" "2020-06-05 00:13:00 GMT"
[5] "2020-06-05 00:14:00 GMT" "2020-06-05 00:15:00 GMT"
[7] "2020-06-05 00:16:00 GMT" "2020-06-05 00:17:00 GMT"
[9] "2020-06-05 00:18:00 GMT" "2020-06-05 00:19:00 GMT"
[11] "2020-06-05 00:20:00 GMT" "2020-06-05 00:21:00 GMT"
[13] "2020-06-05 00:22:00 GMT" "2020-06-05 00:23:00 GMT"
[15] "2020-06-05 00:24:00 GMT" "2020-06-05 00:25:00 GMT"
[17] "2020-06-05 00:26:00 GMT" "2020-06-05 00:27:00 GMT"
[19] "2020-06-05 00:28:00 GMT" "2020-06-05 00:29:00 GMT"
[21] "2020-06-05 00:30:00 GMT" "2020-06-05 00:31:00 GMT"
[23] "2020-06-05 00:32:00 GMT" "2020-06-05 00:33:00 GMT"
[25] "2020-06-05 00:34:00 GMT" "2020-06-05 00:35:00 GMT"
[27] "2020-06-05 00:36:00 GMT" "2020-06-05 00:37:00 GMT"
[29] "2020-06-05 00:38:00 GMT" "2020-06-05 00:39:00 GMT"
[31] "2020-06-05 00:40:00 GMT" "2020-06-05 00:41:00 GMT"
[33] "2020-06-05 00:42:00 GMT" "2020-06-05 00:43:00 GMT"
[35] "2020-06-05 00:44:00 GMT" "2020-06-05 00:45:00 GMT"
[37] "2020-06-05 00:46:00 GMT" "2020-06-05 00:47:00 GMT"
[39] "2020-06-05 00:48:00 GMT" "2020-06-05 00:49:00 GMT"
[41] "2020-06-05 00:50:00 GMT"
raja
Published on 17-Oct-2020 09:37:54
Advertisements