如何调整数值向量以强制其总和等于1L?

时间:2014-07-30 12:02:53

标签: r math

vec = c(1.349667e-04,1.969667e-04,4.866497e-02,6.196667e-05,3.560097e-02,7.796667e-05, 9.896667e-05,1.035540e-01,1.127967e-03,1.361497e-02,1.997300e-01,1.249667e-04,1.877997e-02,1.501250e-01,2.405967e-03,1.218597e-02, 1.354810e-01,2.179667e-04,2.119967e-03,9.272497e-02,4.696967e-05,1.399667e-04,8.739897e-02,5.885497e-02,2.222497e-02,1.709667e-04, 1.124497e-02,2.441967e-03,4.049667e-04,4.196667e-05)

> sum(vec)
[1] 1
> sum(vec)==1L
[1] FALSE

如何稍微调整此向量的每个值,使其总和为1L?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

> sum(vec/sum(vec))==1L
[1] TRUE

你应该阅读FAQ 7.31