sample_date: Draw a sample of n random dates (from a given range).

View source: R/data_fun.R

sample_dateR Documentation

Draw a sample of n random dates (from a given range).

Description

sample_date draws a sample of n random dates from a given range.

Usage

sample_date(from = "1970-01-01", to = Sys.Date(), size = 1, ...)

Arguments

from

Earliest date (as "Date" or string). Default: from = "1970-01-01" (as a scalar).

to

Latest date (as "Date" or string). Default: to = Sys.Date() (as a scalar).

size

Size of date samples to draw. Default: size = 1.

...

Other arguments. (Use for specifying replace, as passed to sample().)

Details

By default, sample_date draws n = 1 random date (as a "Date" object) in the range from = "1970-01-01" to = Sys.Date() (current date).

Both from and to currently need to be scalars (i.e., with a length of 1).

Value

A vector of class "Date".

See Also

Other sampling functions: coin(), dice_2(), dice(), sample_char(), sample_time()

Examples

sample_date()
sort(sample_date(size = 10))
sort(sample_date(from = "2020-02-28", to = "2020-03-01", 
     size = 10, replace = TRUE))  # 2020 is a leap year

# Note: Oddity with sample():
sort(sample_date(from = "2020-01-01", to = "2020-01-01", size = 10, replace = TRUE))  # range of 0!
# see sample(9:9, size = 10, replace = TRUE)


ds4psy documentation built on Sept. 15, 2023, 9:08 a.m.