我有一个简短的脚本,它用两个不同的分布进行采样来模拟物理过程。看评论。我怎样才能将迭代结果放入矩阵进行进一步的统计分析?我查看了以前回答的问题,但仍然无法使其工作。我明白,如果循环不是R中的首选方法,但循环是我基于基本的Perl和Python理解的,其他所有内容都会让我困惑。R新手:将一个循环的输出保存到一个矩阵中
library(truncnorm)
library(mc2d)
o <- 0.04
n <- 10 # number of random samples - kept low for debugging
md <- seq(0,0.70,by=0.05) # md for mode in the PERT distribution
for(i in md) { # iterates over all modes in PERT distribution
f <- rpert(n, min=0, mode=md, max=.99, shape=4) # samples from PERT distribution
a <- rtruncnorm(n, a=0, b=Inf, mean = 5.44, sd = 0.43) # samples from normal distribution
ma <- a*(1-f)+ f*o # calculates results
print(ma) # I need this in a matrix
}
忘记你从其他编程语言知道什么。相反,记住你的代数课并思考向量。如果你想为矢量的每一个元素做一些事情,很有可能有一个函数可以一次完成整个矢量。 – Roland
嗨@Klaus,下面的答案是否回答你的问题?如果是这样,请随时通过点击旁边的“v”符号来接受您认为最有用的答案。另请参阅http://stackoverflow.com/tour如果您的问题尚未解决,请随时提供有关不起作用的说明。 – coffeinjunky