##Mã đề: 48785 Câu 1 (5 điểm): Dùng R đê: Tính trung bình, phương sai và độ lệch chuẩn của sepal.Width cho tất cả các dòng dữ liệu. Vẽ biểu đồ scatter giữa Petal. Length và Petal.Width, phân loại theo Species bằng ggplot2.

# Load thư viện
library(ggplot2)

# Dữ liệu iris có sẵn trong R
data(iris)

# 1. Tính trung bình, phương sai, độ lệch chuẩn của Sepal.Width
mean_sepal_width <- mean(iris$Sepal.Width)
var_sepal_width  <- var(iris$Sepal.Width)
sd_sepal_width   <- sd(iris$Sepal.Width)

mean_sepal_width
## [1] 3.057333
var_sepal_width
## [1] 0.1899794
sd_sepal_width
## [1] 0.4358663
# 2. Vẽ biểu đồ scatter
library(ggplot2)

data(iris)

ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color = Species)) +
  geom_point(size = 2) +
  labs(
    title = "Scatter plot: Petal.Length vs Petal.Width",
    x = "Petal Length",
    y = "Petal Width"
  ) +
  
  theme_minimal()

##Câu 2 (5 điểm): Dùng Pandas để: Đọc file iris.csv. Tính tổng giá trị của Sepal. Length và Sepal.Width theo từng loại Species. Xuất kết quả ra file total_by_species.csv.

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

# 1. Đoc file iris.csv
df = pd.read_csv("iris.csv")

print(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. Tinh tong Sepal.Length va Sepal.Width theo Species
total_by_species = df.groupby(
    "Species", observed=True
)[["Sepal.Length", "Sepal.Width"]].sum()

# 3. Xuat ket qua ra file CSV
total_by_species.to_csv("total_by_species.csv", index=True)

# 4. In ket qua ra man hinh
print(total_by_species)
##             Sepal.Length  Sepal.Width
## Species                              
## setosa             250.3        171.4
## versicolor         296.8        138.5
## virginica          329.4        148.7