Câu 1: Dùng R để

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

2. Vẽ biểu đồ line của trung bình Sepal.Width theo từng nhóm Species sử dụng ggplot2.

Bài làm

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

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
iris %>%
  group_by(Species) %>%
  summarise(
    min_petal_width = min(Petal.Width),
    max_petal_width = max(Petal.Width)
  )
## # 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

2. Vẽ biểu đồ line của trung bình Sepal.Width theo Species sử dụng ggplot2.

Bước 1. Tính trung bình Sepal.Width theo Species

mean_sepal <- iris %>%
  group_by(Species) %>%
  summarise(mean_sepal_width = mean(Sepal.Width))

Bước 2. Vẽ biểu đồ line bằng ggplot2

library(ggplot2)

ggplot(mean_sepal, aes(x = Species, y = mean_sepal_width, group = 1)) +
  geom_line() +
  geom_point() +
  labs(
    title = "Mean Sepal.Width by Species",
    x = "Species",
    y = "Mean Sepal.Width"
  )

Câu 2: Dùng Pandas để:

1. Đọc file iris.csv

2. Nhóm dữ liệu theo Species và tính trung bình của tất cả các cột (trừ Species)

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

Bài làm

Xuất dataset iris ra file CSV để Pandas sử dụng

write.csv(iris, "iris.csv", row.names = FALSE)

Load gói reticulate để chạy Pandas trên R Studio

library(reticulate)
py_config()
## python:         C:/Users/ADMIN/AppData/Local/R/cache/R/reticulate/uv/cache/archive-v0/ICq2rdXOdD_SW4EMvR9K5/Scripts/python.exe
## libpython:      C:/Users/ADMIN/AppData/Local/R/cache/R/reticulate/uv/python/cpython-3.12.12-windows-x86_64-none/python312.dll
## pythonhome:     C:/Users/ADMIN/AppData/Local/R/cache/R/reticulate/uv/cache/archive-v0/ICq2rdXOdD_SW4EMvR9K5
## virtualenv:     C:/Users/ADMIN/AppData/Local/R/cache/R/reticulate/uv/cache/archive-v0/ICq2rdXOdD_SW4EMvR9K5/Scripts/activate_this.py
## version:        3.12.12 (main, Dec 17 2025, 21:10:14) [MSC v.1944 64 bit (AMD64)]
## Architecture:   64bit
## numpy:          C:/Users/ADMIN/AppData/Local/R/cache/R/reticulate/uv/cache/archive-v0/ICq2rdXOdD_SW4EMvR9K5/Lib/site-packages/numpy
## numpy_version:  2.4.0
## 
## NOTE: Python version was forced by VIRTUAL_ENV

1. Đọc file iris.csv

import pandas as pd

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. Nhóm dữ liệu theo Species và tính trung bình của tất cả 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

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

mean_by_species.to_csv("mean_by_species.csv")