# Simulating categorical random variables in R

October 5, 2012

No base R solution with syntactic sugar. Plenty of suggestions. Here’s the ‘best’:

```
sample(letters[1:3], 1, replace = TRUE) # equal probability
sample(letters[1:3], 1, prob = c(.25, .25, .75), replace = TRUE) # unequal probability
sample(letters[1:3], 10, prob = c(.25, .25, .75), replace = TRUE) # iid sample
```

The unfortunate thing is that you have to specify the sample size as the second argument (or name it), unlike the standard `rnorm`

, `rbinom`

, etc. family of functions.

Advertisements

## Trackbacks