【发布时间】:2013-11-29 20:33:33
【问题描述】:
我需要一些非常简单的东西,在我看来我的解决方案有太多步骤(考虑到 R 是为统计而完成的):
我只需要一个包含 N 个概率权重(介于 0 和 1 之间)的数组,总和为 1。什么是简单和/或有效的解决方案?
我的解决方案:
N <- 40
weights <- runif(40)
f <- 1/sum(weights)
weights <- f*weights
print(sum(weights))
【问题讨论】:
-
prop.table(runif(N)) -
太棒了!你为什么不回答让我接受?
-
您正在添加一个额外的步骤。为什么不只是;
weights <- runif(40); w <- weights / sum(weights)?
标签: r random probability