使用RStudio 从均值为200、标准差为10的正态总体中产生1000个随机数,并将这1000个数据分成组距为10的组,生成频数分布表
时间: 2024-09-08 10:04:35 浏览: 263
在R Studio中生成均值为200、标准差为10的正态总体的1000个随机数,并将它们分组并制作频数分布表,可以按照以下步骤操作:
1. 首先,需要安装和加载必要的包,如`ggplot2`用于绘图,`dplyr`处理数据。如果尚未安装,可以运行:
```R
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("dplyr")) install.packages("dplyr")
library(ggplot2)
library(dplyr)
```
2. 然后,使用`rnorm()`函数生成随机数样本:
```R
set.seed(123) # 设置随机种子保证结果可复现
random_numbers <- rnorm(1000, mean = 200, sd = 10)
```
3. 接下来,创建等宽分组的范围,这里设为每组10个单位:
```R
bin_width <- 10
bins <- seq(from = min(random_numbers) - 5, to = max(random_numbers) + 5, by = bin_width)
```
4. 将随机数分配到各个组,并计算每个组的频数(即出现次数):
```R
frequency_table <- tibble(numbers = random_numbers, bins = cut(random_numbers, breaks = bins)) %>%
group_by(bins) %>%
summarize(frequency = n())
```
5. 最后,利用`ggplot2`绘制频数分布表(直方图):
```R
histogram <- ggplot(data = frequency_table, aes(x = bins, y = frequency)) +
geom_bar(stat = "identity", fill = "blue") +
labs(title = "Random Numbers Frequency Distribution",
x = "Number of Random Values (Binned by 10)",
y = "Frequency")
histogram
```
阅读全文