Random categorical data based on probability vector

Hi, I suspect this question is possibly quite basic.

I have a probability vector like the following:

probs <- c(0.1, 0.4, 0.2, 0.3)

These values correspond to the probability of a value being a 1, 2, 3 or 4 (based on their position in the vector). IE The probability of 4 is 0.3.

What I am looking to do is generate N random draws of these values based on these probabilities. I was imagining something akin to the likes of rbinom or rnorm but I can't seem to find anything like that for categorical data.

Is there an easy / good way of doing this?

Thanks!

Type

? sample

In your R console

1 Like

Ah yep, of course you can do it that way. Thanks very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.