Câu 1:
# Đặt ngẫu nhiên
set.seed(123)
n <- 20
p <- 0.5
data_bern <- rbinom(600, n, p)
# Vẽ histogram
hist(data_bern, breaks = 15, probability = TRUE, col = "skyblue",
main = "Histogram và Phân phối Chuẩn", xlab = "Giá trị", border = "white")
# Tạo phân phối chuẩn
x <- seq(min(data_bern), max(data_bern), length = 100)
y <- dnorm(x, mean = n * p, sd = sqrt(n * p * (1 - p)))
# Vẽ phân phối chuẩn
lines(x, y, col = "red", lwd = 2)
# chú thích
legend("topright", legend = c("Histogram", "Phân phối chuẩn"), col = c("skyblue", "red"), lwd = 2)

Câu 2:
Đọc dữ liệu Iris
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Đọc dữ liệu Iris
data(iris)
head(iris)
## 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
iris_summary <- iris %>%
group_by(Species) %>%
summarise(
Sepal.Length.Mean = mean(Sepal.Length),
Sepal.Width.Mean = mean(Sepal.Width),
Petal.Length.Mean = mean(Petal.Length),
Petal.Width.Mean = mean(Petal.Width),
Sepal.Length.SD = sd(Sepal.Length),
Sepal.Width.SD = sd(Sepal.Width),
Petal.Length.SD = sd(Petal.Length),
Petal.Width.SD = sd(Petal.Width)
)
print(iris_summary)
## # A tibble: 3 × 9
## Species Sepal.Length.Mean Sepal.Width.Mean Petal.Length.Mean Petal.Width.Mean
## <fct> <dbl> <dbl> <dbl> <dbl>
## 1 setosa 5.01 3.43 1.46 0.246
## 2 versico… 5.94 2.77 4.26 1.33
## 3 virgini… 6.59 2.97 5.55 2.03
## # ℹ 4 more variables: Sepal.Length.SD <dbl>, Sepal.Width.SD <dbl>,
## # Petal.Length.SD <dbl>, Petal.Width.SD <dbl>
Vẽ biểu đồ Boxplot của chiều rộng đài hoa (Sepal.Width)
ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Boxplot của chiều rộng đài hoa (Sepal.Width)",
x = "Loài hoa",
y = "Chiều rộng đài hoa")
