Learn R Programming

ds4psy (version 0.2.0)

sample_times: Draw a sample of n random times (from a given range).

Description

sample_times draws a sample of n random times from a given range.

Usage

sample_times(n = 1, from = "1970-01-01 00:00:00", to = Sys.time())

Arguments

n

Number dates to draw. Default: n = 1.

from

Earliest date (as string). Default: from = "1970-01-01 00:00:00".

to

Latest date (as string). Default: to = Sys.time().

Details

By default, sample_times draws n = 1 random time in the range from = "1970-01-01 00:00:00" to = Sys.time() (current time).

See Also

Other random functions: coin(), dice_2(), dice(), sample_dates()

Examples

Run this code
# NOT RUN {
# Basics:
sample_times()
sample_times(n = 10)

# Specific ranges:
sort(sample_times(n = 10, from = (Sys.time() - 60)))  # within the last minute
sort(sample_times(n = 10, from = (Sys.time() - 1 * 60 * 60)))  # within the last hour
sort(sample_times(n = 10, from = Sys.time(), 
                            to = (Sys.time() + 1 * 60 * 60)))  # within the next hour
sort(sample_times(n = 10, from = "2020-01-01 00:00:00 CET", 
                            to = "2020-01-01 00:00:01 CET"))  # within 1 sec range
 
# Note: Oddity with sample(): 
sort(sample_times(n = 10, from = "2020-01-01 00:00:00 CET", 
                            to = "2020-01-01 00:00:00 CET"))  # range of 0!
# see sample(9:9, size = 10, replace = TRUE)

# }

Run the code above in your browser using DataLab