sample_char: Draw a sample of n random characters (from given characters).

View source: R/data_fun.R

sample_charR Documentation

Draw a sample of n random characters (from given characters).

Description

sample_char draws a sample of n random characters from a given range of characters.

Usage

sample_char(x_char = c(letters, LETTERS), n = 1, replace = FALSE, ...)

Arguments

x_char

Population of characters to sample from. Default: x_char = c(letters, LETTERS).

n

Number of characters to draw. Default: n = 1.

replace

Boolean: Sample with replacement? Default: replace = FALSE.

...

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

Details

By default, sample_char draws n = 1 a random alphabetic character from x_char = c(letters, LETTERS).

As with sample(), the sample size n must not exceed the number of available characters nchar(x_char), unless replace = TRUE (i.e., sampling with replacement).

Value

A text string (scalar character vector).

See Also

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

Examples

sample_char()  # default
sample_char(n = 10)
sample_char(x_char = "abc", n = 10, replace = TRUE)
sample_char(x_char = c("x y", "6 9"), n =  6, replace = FALSE)
sample_char(x_char = c("x y", "6 9"), n = 20, replace = TRUE)

# Biased sampling: 
sample_char(x_char = "abc", n = 20, replace = TRUE, 
             prob = c(3/6, 2/6, 1/6))

# Note: By default, n must not exceed nchar(x_char):
sample_char(n = 52, replace = FALSE)    # works, but
# sample_char(n = 53, replace = FALSE)  # would yield ERROR; 
sample_char(n = 53, replace = TRUE)     # works again.


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