Question

I want to assign subjects to classes based on probabilities that I provide. I will be doing this in a variety of cases, with different values. Sometimes, I want the probability of a particular class to be 0. I've been using

classlist <- cut(runif(p), c(0, pdrop, ptitrate, pcomplete, pnoise, 1), labels = c("D", "T", "C", "N", "O"))

but this fails when two of the p variables are the same. I could make them different by minimal amounts e.g. pdrop = .2 ptitrate = .200001. But is there some better way?

Thanks

Peter

Was it helpful?

Solution

I suggest sample():

> p         <- 100
> groups    <- c("D", "T", "C", "N", "O")
> probVec   <- c(0.2, 0.2, 0.3, 0.25, 0.05)
> classlist <- factor(sample(groups, size=p, replace=TRUE, prob=probVec))
> table(classlist)
classlist
 C  D  N  O  T 
26 16 28  5 25
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top