开发者

Problem with cut() in R

开发者 https://www.devze.com 2023-02-08 07:23 出处:网络
I want to assign subjects to classes based on probabilities t开发者_如何学Chat I provide. I will be doing this in a variety of cases, with different values. Sometimes, I want the probability of a part

I want to assign subjects to classes based on probabilities t开发者_如何学Chat 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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消