Learn R Programming

ds4psy (version 1.0.0)

sample_char: 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, ...)

Value

A text string (scalar character vector).

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).

See Also

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

Examples

Run this code
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.

Run the code above in your browser using DataLab