问题描述
当我调用 rnorm
传递单个值作为平均值时,很明显会发生什么:一个值是从 Normal(10,1) 生成的.
When I call rnorm
passing a single value as mean, it's obvious what happens: a value is generated from Normal(10,1).
y <- rnorm(20, mean=10, sd=1)
但是,我看到了整个向量被传递给 rnorm
(或 rcauchy
等)的例子;在这种情况下,我不确定 R 机器的真正作用.例如:
But, I see examples of a whole vector being passed to rnorm
(or rcauchy
, etc..); in this case, I am not sure what the R machinery really does. For example:
a = c(10,22,33,44,5,10,30,22,100,45,97)
y <- rnorm(a, mean=a, sd=1)
有什么想法吗?
推荐答案
rnorm
生成的随机数的数量等于 a 的长度.来自 ?norm
:
The number of random numbers rnorm
generates equals the length of a. From ?rnorm
:
n:观察次数.如果‘length(n) > 1’,取长度成为所需的数字.
要查看将 a
传递给 mean 参数时发生了什么,如果我们更改示例会更容易:
To see what is happening when a
is passed to the mean argument, it's easier if we change the example:
a = c(0, 10, 100)
y = rnorm(a, mean=a, sd=1)
[1] -0.4853138 9.3630421 99.7536461
所以我们生成 length(a)
个均值 a[i]
的随机数.
So we generate length(a)
random numbers with mean a[i]
.
这篇关于用均值向量调用 rnorm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!