Skip to contents

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

Usage

sim_double(
  x = runif(n = 10L, min = -10, max = 10),
  length = c(1L, 10L),
  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

(Double-precision 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_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

Double-precision vector

Details

The arguments list contains many possible conditions. Combining more than one set of conditions may not necessarily result in the specified behavior. For example, sim_double(prob_null=0.9, prob_zero_length=0.9) does not result in NULL and zero-length outcomes each with probability 0.9.

Examples

#----------------------------------------------------------------------------
# sim_double() examples
#----------------------------------------------------------------------------
library(bkcheck)

sim_double()
#> [1]  9.388087 -3.395493  3.412463  9.388087 -6.148026 -4.091817  0.000000
sim_double(prob_na = 1)
#>  [1]  9.699435  5.909007  4.727261  7.220465  2.829648 -2.716598  4.727261
#>  [8]        NA  0.000000  4.727261
sim_double(length = 4, prob_na = 1, prob_nan = 1, prob_inf = 1)
#> [1]      Inf      NaN       NA 8.923015
sim_double(prob_null = 1)
#> NULL
sim_double(prob_zero_length = 1)
#> numeric(0)
sim_double(names = TRUE)
#>    name_1    name_2    name_3 
#> -8.162032  0.000000 -3.119739