generate random time data that is uniformly distributed using R

Viewed 1065

Given a start time 7:00, and the end time 8:00 on date 2017-10-08, need to generate a set of 100 random time data that is uniformly distributed between 7:00 and 8:00. Wondered how to realise this using R?

2 Answers

Maybe this can help you.

as.POSIXct(" 2017-10-08 07:00:00") + runif(n=100, min=0, max=3600)

Output:

  [1] "2017-10-08 07:11:22 -03" "2017-10-08 07:56:56 -03" "2017-10-08 07:32:49 -03" "2017-10-08 07:10:02 -03" "2017-10-08 07:26:02 -03"
  [6] "2017-10-08 07:12:55 -03" "2017-10-08 07:38:02 -03" "2017-10-08 07:13:53 -03" "2017-10-08 07:53:55 -03" "2017-10-08 07:45:38 -03"
 [11] "2017-10-08 07:10:45 -03" "2017-10-08 07:12:59 -03" "2017-10-08 07:04:53 -03" "2017-10-08 07:48:45 -03" "2017-10-08 07:51:55 -03"
 [16] "2017-10-08 07:15:12 -03" "2017-10-08 07:46:31 -03" "2017-10-08 07:26:50 -03" "2017-10-08 07:27:28 -03" "2017-10-08 07:42:02 -03"
 [21] "2017-10-08 07:11:13 -03" "2017-10-08 07:35:57 -03" "2017-10-08 07:42:43 -03" "2017-10-08 07:40:35 -03" "2017-10-08 07:30:59 -03"
 [26] "2017-10-08 07:51:16 -03" "2017-10-08 07:11:29 -03" "2017-10-08 07:23:11 -03" "2017-10-08 07:34:58 -03" "2017-10-08 07:37:49 -03"
 [31] "2017-10-08 07:49:24 -03" "2017-10-08 07:27:57 -03" "2017-10-08 07:37:27 -03" "2017-10-08 07:17:23 -03" "2017-10-08 07:00:49 -03"
 [36] "2017-10-08 07:14:36 -03" "2017-10-08 07:05:32 -03" "2017-10-08 07:15:23 -03" "2017-10-08 07:33:12 -03" "2017-10-08 07:15:34 -03"
 [41] "2017-10-08 07:14:20 -03" "2017-10-08 07:17:27 -03" "2017-10-08 07:47:07 -03" "2017-10-08 07:55:27 -03" "2017-10-08 07:56:49 -03"
 [46] "2017-10-08 07:28:29 -03" "2017-10-08 07:04:51 -03" "2017-10-08 07:38:32 -03" "2017-10-08 07:50:14 -03" "2017-10-08 07:57:57 -03"
 [51] "2017-10-08 07:58:50 -03" "2017-10-08 07:58:44 -03" "2017-10-08 07:20:31 -03" "2017-10-08 07:29:57 -03" "2017-10-08 07:52:57 -03"
 [56] "2017-10-08 07:12:29 -03" "2017-10-08 07:00:36 -03" "2017-10-08 07:33:28 -03" "2017-10-08 07:40:45 -03" "2017-10-08 07:12:52 -03"
 [61] "2017-10-08 07:04:53 -03" "2017-10-08 07:43:30 -03" "2017-10-08 07:55:45 -03" "2017-10-08 07:46:15 -03" "2017-10-08 07:47:29 -03"
 [66] "2017-10-08 07:42:28 -03" "2017-10-08 07:43:54 -03" "2017-10-08 07:04:10 -03" "2017-10-08 07:30:10 -03" "2017-10-08 07:06:09 -03"
 [71] "2017-10-08 07:14:43 -03" "2017-10-08 07:38:22 -03" "2017-10-08 07:39:23 -03" "2017-10-08 07:36:41 -03" "2017-10-08 07:22:37 -03"
 [76] "2017-10-08 07:30:30 -03" "2017-10-08 07:28:44 -03" "2017-10-08 07:23:47 -03" "2017-10-08 07:10:21 -03" "2017-10-08 07:24:31 -03"
 [81] "2017-10-08 07:09:41 -03" "2017-10-08 07:14:44 -03" "2017-10-08 07:22:36 -03" "2017-10-08 07:42:37 -03" "2017-10-08 07:04:02 -03"
 [86] "2017-10-08 07:03:18 -03" "2017-10-08 07:09:34 -03" "2017-10-08 07:59:47 -03" "2017-10-08 07:54:39 -03" "2017-10-08 07:09:47 -03"
 [91] "2017-10-08 07:43:30 -03" "2017-10-08 07:31:38 -03" "2017-10-08 07:16:34 -03" "2017-10-08 07:45:48 -03" "2017-10-08 07:44:07 -03"
 [96] "2017-10-08 07:09:02 -03" "2017-10-08 07:19:53 -03" "2017-10-08 07:07:28 -03" "2017-10-08 07:35:23 -03" "2017-10-08 07:38:06 -03"

You can use runif or sample functions for this:

time.start <- as.POSIXct('2017-10-08T07:00:00z', format = "%Y-%m-%dT%H:%M:%S")
time.end <- as.POSIXct('2017-10-08T08:00:00z', format = "%Y-%m-%dT%H:%M:%S")

seconds <- difftime(time.end, time.start, units = "secs")
# Option with runif()
v <- round(runif(1000, 0, seconds))

# Option with sample()
v <- sample(1:seconds, 1000, replace = T)

time.uniform <- time.start + v
Related