我需要一些非常简单的东西,在我看来,我的解决方案步骤太多(考虑到 R 是为统计而做的):
I need something very simple and it seems to me that my solution has too many steps (considering that R is done for statistics):
我只需要一个总和为 1 的 N 个概率权重数组(在 0 和 1 之间).什么是简单和/或有效的解决方案?
All I need is an array of N probability weights (between 0 and 1) that sum up to one. What would be an easy and/or efficient solution?
我的解决方案:
N <- 40 weights <- runif(40) f <- 1/sum(weights) weights <- f*weights print(sum(weights)) 推荐答案解决方案:
您可以使用 prop.table 函数和 runif 函数:
You can use prop.table function and runif function:
weights <- prop.table(runif(N))这和跑步一样
w <- runif(N); weights <- w / sum(w)说明: