View source: R/distributions.r
rcategorical | R Documentation |
Allows different class probabilities for each person by supplying a matrix with one column for each class and one row for each person.
rcategorical(n, probs, labels=NULL, output="numeric",
reference=NULL)
n |
How many draws to make. Passed to the |
probs |
Either a numeric vector of probabilities which sums to one or a matrix with one column for each desired class and |
labels |
A vector of labels to draw from. If |
output |
A single character string specifying the output format of the results. Must be either |
reference |
A single character string, specifying which of the possible values should be considered the reference when |
In case of a simple numeric vector (class probabilities should be the same for all draws), this function is only a wrapper for the sample
function, to make the code more consistent. It uses weighted sampling with replacement. Otherwise, custom code is used which is faster than the standard rmultinom
function.
Returns a numeric vector (or factor vector if coerce2factor=TRUE
) of length n
.
Robin Denz
library(simDAG)
rcategorical(n=5, labels=c("A", "B", "C"), probs=c(0.1, 0.2, 0.7))
rcategorical(n=2, probs=matrix(c(0.1, 0.2, 0.5, 0.7, 0.4, 0.1), nrow=2))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.