Skip to contents

Simulate a vector of random date values (class Date). Provides the option of inserting bkcheck:::NA_Date_ or returning base::NULL or zero-length dates.

Usage

sim_date(
  x = seq(from = as.Date(Sys.Date() - 30), to = as.Date(Sys.Date() + 30), by = "day"),
  length = c(1, 10),
  prob_na = 0,
  n_na = 1L,
  prob_null = 0,
  prob_zero_length = 0,
  names = FALSE,
  replace = TRUE
)

Arguments

x

(class Date vector)
The vector to randomly sample from.

length

(integer vector of length 1 or 2)
The number of elements in the simulated vector. If length(length)==1L, sets the number of elements to length. If length(length)==2L, randomly selects the number of elements from the interval [min, max].

prob_na

(Scalar numeric: [0, 1])
The probability of inserting an NA.

n_na

(Scalar integer: [0, Inf])
The number of NAs to insert.

prob_null

(Scalar numeric: [0, 1])
The probability of returning a NULL object.

prob_zero_length

(Scalar numeric: [0, 1])
The probability of returning a zero-length vector.

names

(Scalar logical: c(TRUE, FALSE))
Whether or not the vector should have names. TRUE (default) sets names as name_1, name_2, name_3, ...

replace

(Scalar logical: c(TRUE, FALSE))
Whether or not elements from x can be repeated in the result. TRUE (default) samples with replacement.

Value

Date vector

Examples

#----------------------------------------------------------------------------
# sim_date() examples
#----------------------------------------------------------------------------
library(bkcheck)

sim_date()
#> [1] "2026-05-24"
sim_date(prob_na = 1)
#> [1] NA
sim_date(prob_null = 1)
#> NULL
sim_date(prob_zero_length = 1)
#> Date of length 0
sim_date(names = TRUE)
#>       name_1       name_2       name_3       name_4       name_5       name_6 
#> "2026-05-27" "2026-05-21" "2026-07-13" "2026-05-27" "2026-05-15" "2026-06-22" 
#>       name_7       name_8       name_9 
#> "2026-07-06" "2026-05-24" "2026-06-29"