将向量与概率分布进行比较

comparing a vector to a probability distribution

我有一个向量:

r <- runif(10)
r
[1] 0.52324423 0.89110751 0.44616915 0.70163640 0.63741495 0.31263977
[7] 0.73947973 0.83278799 0.04971461 0.01820381

我也有概率分布

p <- c(0, cumsum(rep(0.25, 4)))
p
[1] 0.00 0.25 0.50 0.75 1.00

我想根据 p 中的概率分布将因子分配给 r。

换句话说,我希望我的输出是:

r
[1] 3  4  2  3  3  2  3  4  1  1

当我尝试这个时,我收到警告:

which( r >= p) -1
[1] 3
Warning message:
In r < p : longer object length is not a multiple of shorter object length

换句话说,只有 r 中的第一个值与 p 进行比较。

我如何将 r 转换为水平向量,然后我可以将其转换为因子?

您可以使用cut

as.integer(cut(r, breaks=p))