Skip to contents

Simulate a vector of random strings. Provides the option of inserting base::NA or returning base::NULL or zero-length objects.

Usage

sim_character(
  x = rand_chars(length = 10),
  length = c(1, 10),
  prob_na = 0,
  n_na = 1L,
  prob_null = 0,
  prob_zero_length = 0,
  names = FALSE,
  replace = TRUE
)

Arguments

x

(character)
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

character

Examples

#----------------------------------------------------------------------------
# sim_character() examples
#----------------------------------------------------------------------------
library(bkcheck)

sim_character()
#> [1] "7So~Vn"  "$xWSX\\" "AHw^qR"  "$xWSX\\" "AHw^qR"  "nRZE1T"  "KUzY5o" 
#> [8] "f#'Pk,"  "$xWSX\\"
sim_character(prob_na = 1)
#> [1] "9\"nXJ9" "7ZpUGa"  NA       
sim_character(prob_null = 1)
#> NULL
sim_character(prob_zero_length = 1)
#> character(0)
sim_character(names = TRUE)
#>   name_1 
#> "Wu|*&(" 

sim_character(replace = FALSE)
#> [1] "AW_(1]"  "r(tU3J"  "hQvHLs"  "P6ALmI"  "$%(L56"  "K'*2CO"  "Yu8b!A" 
#> [8] "5q~S,U"  "&\\J+fn"
sim_character(replace = TRUE)
#> [1] "6Hm^w3"  "6Hm^w3"  "Y^}A\\P" "%S$}9T"  "beJ9]."  "NzlYzg"  "HoZ5p."