Skip to contents

Simulate a vector of random integer values. Provides the option of inserting zeros, base::NA, base::NaN, and base::Inf values or returning base::NULL or zero-length objects.

Usage

sim_integer(
  x = -10:10,
  length = c(1, 10),
  prob_zero = 1,
  n_zero = 1L,
  prob_na = 0,
  n_na = 1L,
  prob_nan = 0,
  n_nan = 1L,
  prob_inf = 0,
  n_inf = 1L,
  prob_null = 0,
  prob_zero_length = 0,
  names = FALSE,
  replace = TRUE
)

Arguments

x

(integer)
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_zero

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

n_zero

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

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_nan

(Scalar numeric: [0, 1])
The probability of inserting a NaN.

n_nan

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

prob_inf

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

n_inf

(Scalar integer: [0, Inf])
The number of Infs 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

integer

Details

Note that inserting base::NaN and base::Inf coerces to double storage type.

Examples

#----------------------------------------------------------------------------
# sim_integer() examples
#----------------------------------------------------------------------------
library(bkcheck)

sim_integer()
#> [1] 4 0 3
sim_integer(prob_na = 1)
#> [1] NA -1  0
sim_integer(prob_na = 1, prob_nan = 1, prob_inf = 1)
#> [1]  NA NaN Inf   0   2 -10
sim_integer(prob_null = 1)
#> NULL
sim_integer(prob_zero_length = 1)
#> integer(0)
sim_integer(names = TRUE)
#>  name_1  name_2  name_3  name_4  name_5  name_6  name_7  name_8  name_9 name_10 
#>      -2       8      -8      -6      10      -5       0      -9     -10       8