[英]How to save 10 different rnorm() in a vector in R?
I need to create a vector, which elements are 10 different normal-distributed random sequences each of length 150. After that for each of these sequences the minimum-, maximum-, mean- and median-value need be calculated.我需要创建一个向量,其元素是 10 个不同的正态分布随机序列,每个序列的长度为 150。之后,对于这些序列中的每一个,需要计算最小值、最大值、平均值和中值。 These 4 statistics need be stored in a vector.
这 4 个统计信息需要存储在一个向量中。 All 4-element-statistics-vectors shall be stored in a data frame.
所有 4 元素统计向量都应存储在数据框中。
i predefined a vector with a length of 10 and was able to create 10 normal distributions with rnorm() and display them using hist().我预定义了一个长度为 10 的向量,并且能够使用 rnorm() 创建 10 个正态分布并使用 hist() 显示它们。 However I think I failed to save them in the vector.
但是我想我没能将它们保存在向量中。
data <- vector(length = 10)
for(i in 1:10) {
data[i] <- hist(rnorm(150, 75, 10))
}
You can get your ten samples like this:你可以这样得到你的十个样本:
samples <- replicate(10, rnorm(150, 75, 10))
And put their summaries into a data frame like this:并将他们的摘要放入这样的数据框中:
do.call(rbind, lapply(asplit(samples, 2), function(x) {
data.frame(min = min(x), max = max(x), mean = mean(x), sd = sd(x))
}))
#> min max mean sd
#> 1 41.63553 107.15836 74.65818 11.691526
#> 2 36.27087 103.41606 74.60017 10.580801
#> 3 53.58147 100.37452 75.35968 10.356784
#> 4 48.44410 98.91955 75.13158 10.403437
#> 5 46.69455 102.77960 73.44792 10.692283
#> 6 52.45185 108.14074 75.42726 9.566448
#> 7 51.96023 98.63533 75.59220 10.520285
#> 8 49.03543 102.96443 73.17830 10.615083
#> 9 45.87507 115.26490 74.54446 10.394427
#> 10 47.32588 96.58989 75.85079 10.282901
Created on 2022-11-14 with reprex v2.0.2创建于 2022-11-14,使用reprex v2.0.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.