Learn R Programming

vws (version 0.3.0)

Categorical: Categorical Distribution

Description

Draw variates from a categorical distribution.

Usage

r_categ(n, p, log = FALSE, one_based = FALSE)

Value

A vector of \(n\) draws.

Arguments

n

Number of desired draws.

p

Vector of \(k\) probabilities for distribution.

log

logical; if TRUE, interpret p as being specified on the log-scale as log(p). Otherwise, interpret p as being specified on the original probability scale.

one_based

logical; if TRUE, assume a categorical distribution with support \(\{ 1, \ldots, k \}\). Otherwise, assume support \(\{ 0, \ldots, k - 1 \}\).

Examples

Run this code
p = c(0.1, 0.2, 0.3, 0.4)
lp = log(p)

set.seed(1234)
r_categ(50, p, log = FALSE, one_based = FALSE)
r_categ(50, p, log = FALSE, one_based = TRUE)

set.seed(1234)
r_categ(50, lp, log = TRUE, one_based = FALSE)
r_categ(50, lp, log = TRUE, one_based = TRUE)

Run the code above in your browser using DataLab