[英]In R, how can I make a vector Y whose components are derived from normal distribution?
I am a novice in R programming. 我是R编程的新手。
I would like to ask experts here a question concerning a code of R. 我想在这里问专家有关R代码的问题。
First, let a vector x
be c(2,5,3,6,5)
首先,让向量
x
为c(2,5,3,6,5)
I hope to make another vector y
whose i-th component is derived from N(sum(x[1]:x[i]),1)
我希望制作另一个向量
y
其第i个分量是从N(sum(x[1]:x[i]),1)
导出的
(ie the i-th component of y follows normal distribuion with variance 1 and mean summation from x[1](=2)
to x[i] (i=1,2,3,4,5)
) (即y的第i个分量服从方差1的正态分布,并且从
x[1](=2)
到x[i] (i=1,2,3,4,5)
均值求和)
For example, the third component of y follows normal distribuion with mean x[1]+x[2]+x[3]=2+5+3=10
and variance 1 例如,y的第三部分遵循正态分布,均值
x[1]+x[2]+x[3]=2+5+3=10
且方差1
I want to know a code of R making the vector y described above "without using repetition syntax such as for, while, etc." 我想知道R的代码,使“不使用for,while等重复语法”上述向量y 成为可能。
Since I am a novice of R programming and have a congenitally poor sense of computational statistics, I don't seem to hit on a ingenious code of R at all. 由于我是R编程的新手,并且对计算统计的理解一直很差,所以我似乎根本没有碰过R的巧妙代码。
Please let me know a code of R making a vector explained above without using repetition syntax such as for, while, etc. 请让我知道上面说明的R构成向量的代码,而没有使用for,while等重复语法。
Previously, I should like to thank you very much heartily for your mindful answer. 以前,我要衷心感谢您的明智回答。
You can do 你可以做
rnorm(length(x), mean = cumsum(x), sd = 1)
rnorm
is part of the family of functions associated with the normal distribution *norm
. rnorm
是与正态分布*norm
相关联的函数系列的一部分。 To see how a function with a known name works, use 要查看具有已知名称的功能如何工作,请使用
help("rnorm") # or ?rnorm
cumsum
takes the cumulative sum of a vector. cumsum
的累加和。
Finding functionality 寻找功能
In R, it's generally a safe bet that most functionality you can think of has been implemented by someone already. 在R中,通常可以肯定的是,您可以想到的大多数功能已经由某人实现。 So, for example, in the OP's case, it is not necessary to roll a custom loop.
因此,例如,在OP的情况下,无需滚动自定义循环。
The same naming convention as *norm
is followed for other distributions, eg, rbinom
. 其他发行版(例如
rbinom
)遵循与*norm
相同的命名约定。 You can follow the link at the bottom of ?rnorm
to reach ?Distributions
, which lists others in base R. 您可以通过
?rnorm
底部的链接访问?Distributions
,该列表列出了基数R中的其他内容。
If you are starting from scratch and don't know the names of any related functions, consider using the built-in search tools, like: 如果您是从头开始的,并且不知道任何相关功能的名称,请考虑使用内置的搜索工具,例如:
help.search("normal distribution") # or ??"normal distribution"
If this reveals nothing and yet you still think a function must exist, consider installing and loading the sos
package, which allows 如果这没有显示任何内容,但您仍然认为必须存在某个功能,请考虑安装和加载
sos
软件包,该软件包可以
findFn("{cumulative mean}") # or ???"{cumulative mean}"
findFn("{the pareto distribution}") # or ???"{the pareto distribution}"
Beyond that, there are other online resources, like Google, that are good. 除此之外,还有其他在线资源(例如Google)也不错。 However, a question about functionality on Stack Overflow is a risky proposition, since it will not be received well (downvoted and closed as a "tool request") if the implementation of the desired functionality is nonexistent or unknown to folks here.
但是,有关堆栈溢出功能的问题是一个冒险的提议,因为如果此处所需的功能的实现不存在或不为人们所知,它将不会被很好地接受(作为“工具请求”被否决并关闭)。 Stack Overflow's new "Documentation" subsite will hopefully prove to be a resource for finding R functions as well.
Stack Overflow的新“ Documentation”子站点有望被证明也是查找R函数的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.