Câu 1 (5 điểm): Dùng R

1.1. Tính giá trị lớn nhất và nhỏ nhất của Petal.Width theo từng Species

# Sử dụng bộ dữ liệu iris có sẵn trong R data(iris)

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
petal_width_stats <- iris %>% group_by(Species) %>% summarise( Min_Petal_Width = min(Petal.Width), Max_Petal_Width = max(Petal.Width) )

petal_width_stats
## # A tibble: 3 × 3
##   Species    Min_Petal_Width Max_Petal_Width
##   <fct>                <dbl>           <dbl>
## 1 setosa                 0.1             0.6
## 2 versicolor             1               1.8
## 3 virginica              1.4             2.5

1.2. Vẽ biểu đồ line của trung bình Sepal.Width theo từng Species (ggplot2)

library(ggplot2)

# Tính trung bình Sepal.Width theo Species

sepal_width_mean <- iris %>%
group_by(Species) %>%
summarise(
Mean_Sepal_Width = mean(Sepal.Width)
)

# Vẽ biểu đồ line

ggplot(sepal_width_mean, aes(x = Species, y = Mean_Sepal_Width, group = 1)) +
geom_line() +
geom_point(size = 3) +
labs(
title = "Trung bình Sepal.Width theo Species",
x = "Species",
y = "Mean Sepal.Width"
) +
theme_minimal()

Câu 2 (5 điểm): Dùng Pandas (Python)

Load data bằng sklearn

data(iris)
write.csv(iris, "iris.csv", row.names = FALSE)
import pandas as pd

# Đọc file iris.csv

df = pd.read_csv("iris.csv")

df.head()
##    Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
## 0           5.1          3.5           1.4          0.2  setosa
## 1           4.9          3.0           1.4          0.2  setosa
## 2           4.7          3.2           1.3          0.2  setosa
## 3           4.6          3.1           1.5          0.2  setosa
## 4           5.0          3.6           1.4          0.2  setosa

2.2. Nhóm theo Species và tính trung bình các cột (trừ Species)

mean_by_species = df.groupby("Species").mean()

mean_by_species
##             Sepal.Length  Sepal.Width  Petal.Length  Petal.Width
## Species                                                         
## setosa             5.006        3.428         1.462        0.246
## versicolor         5.936        2.770         4.260        1.326
## virginica          6.588        2.974         5.552        2.026

2.3. Lưu kết quả vào file mean_by_species.csv

mean_by_species.to_csv("mean_by_species.csv")

print("Đã lưu file mean_by_species.csv")
## Đã lưu file mean_by_species.csv