Tải thư viện

library(ggplot2)

Data

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

Câu 1: Phân tích dữ liệu Iris bằng R

a) Tính giá trị lớn nhất, nhỏ nhất và độ lệch chuẩn của Sepal.Width theo Species

aggregate(
  Sepal.Width ~ Species,
  data = iris,
  FUN = function(x) c(
    Min = min(x),
    Max = max(x),
    SD  = sd(x)
  )
)
##      Species Sepal.Width.Min Sepal.Width.Max Sepal.Width.SD
## 1     setosa       2.3000000       4.4000000      0.3790644
## 2 versicolor       2.0000000       3.4000000      0.3137983
## 3  virginica       2.2000000       3.8000000      0.3224966

b) Vẽ boxplot Sepal.Length theo từng nhóm Species bằng ggplot2

ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot() +
labs(title = "Boxplot Sepal.Length theo Species",
x = "Species",
y = "Sepal.Length"
)

Câu 2:

a) Đọc file iris.csv, đếm số dòng có Petal.Length > 4.0, ghi ra file

import pandas as pd

# Đọc file
df = pd.read_csv("iris.csv")

# Lọc Petal.Length > 4.0
filtered = df[df["Petal.Length"] > 4.0]

# Đếm số dòng
count = len(filtered)

# Xuất kết quả ra file
result = pd.DataFrame({"count": [count]})
result.to_csv("count_filtered_petal_length.csv", index=False)