# 描述数据集
data(iris)
str(iris)
head(iris)
# 不同植物样本数
table(iris$Species)
# 汇总鸢尾属和不同品种的平均数、中位数等特征值
summary_stats <- aggregate(. ~ Species, data = iris, FUN = function(x) c(N = length(x), Mean = mean(x), Median = median(x), SD = sd(x)))
summary_stats
# 绘制箱型图和频率直方图
par(mfrow = c(2, 2))
boxplot(Sepal.Length ~ Species, data = iris, main = "萼片长度的箱型图", xlab = "物种", ylab = "萼片长度")
boxplot(Sepal.Width ~ Species, data = iris, main = "萼片宽度的箱型图", xlab = "物种", ylab = "萼片宽度")
boxplot(Petal.Length ~ Species, data = iris, main = "花瓣长度的箱型图", xlab = "物种", ylab = "花瓣长度")
boxplot(Petal.Width ~ Species, data = iris, main = "花瓣宽度的箱型图", xlab = "物种", ylab = "花瓣宽度")

# 选择两个品种
setosa_data <- iris[iris$Species == "setosa", ]
versicolor_data <- iris[iris$Species == "versicolor", ]
# 对花瓣长度进行t检验
t_test_length <- t.test(setosa_data$Petal.Length, versicolor_data$Petal.Length)
t_test_length
# 对花瓣宽度进行t检验
t_test_width <- t.test(setosa_data$Petal.Width, versicolor_data$Petal.Width)
t_test_width
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
##
## setosa versicolor virginica
## 50 50 50
## Species Sepal.Length.N Sepal.Length.Mean Sepal.Length.Median
## 1 setosa 50.0000000 5.0060000 5.0000000
## 2 versicolor 50.0000000 5.9360000 5.9000000
## 3 virginica 50.0000000 6.5880000 6.5000000
## Sepal.Length.SD Sepal.Width.N Sepal.Width.Mean Sepal.Width.Median
## 1 0.3524897 50.0000000 3.4280000 3.4000000
## 2 0.5161711 50.0000000 2.7700000 2.8000000
## 3 0.6358796 50.0000000 2.9740000 3.0000000
## Sepal.Width.SD Petal.Length.N Petal.Length.Mean Petal.Length.Median
## 1 0.3790644 50.0000000 1.4620000 1.5000000
## 2 0.3137983 50.0000000 4.2600000 4.3500000
## 3 0.3224966 50.0000000 5.5520000 5.5500000
## Petal.Length.SD Petal.Width.N Petal.Width.Mean Petal.Width.Median
## 1 0.1736640 50.0000000 0.2460000 0.2000000
## 2 0.4699110 50.0000000 1.3260000 1.3000000
## 3 0.5518947 50.0000000 2.0260000 2.0000000
## Petal.Width.SD
## 1 0.1053856
## 2 0.1977527
## 3 0.2746501
##
## Welch Two Sample t-test
##
## data: setosa_data$Petal.Length and versicolor_data$Petal.Length
## t = -39.493, df = 62.14, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -2.939618 -2.656382
## sample estimates:
## mean of x mean of y
## 1.462 4.260
##
##
## Welch Two Sample t-test
##
## data: setosa_data$Petal.Width and versicolor_data$Petal.Width
## t = -34.08, df = 74.755, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -1.143133 -1.016867
## sample estimates:
## mean of x mean of y
## 0.246 1.326