Câu 1

# tính trung bình phương sai của tất cả các cột(trừ cột Species)
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
numeric_data <- iris[, sapply(iris, is.numeric)]
head(numeric_data)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1          5.1         3.5          1.4         0.2
## 2          4.9         3.0          1.4         0.2
## 3          4.7         3.2          1.3         0.2
## 4          4.6         3.1          1.5         0.2
## 5          5.0         3.6          1.4         0.2
## 6          5.4         3.9          1.7         0.4
mean_values <- colMeans(numeric_data)
var_values <- apply(numeric_data, 2, var)
mean_values
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
##     5.843333     3.057333     3.758000     1.199333
var_values
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
##    0.6856935    0.1899794    3.1162779    0.5810063
# vẽ biểu đồ phân tán
library(ggplot2)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 2) +
  labs(
    title = "Biểu đồ phân tán Sepal.Length và Sepal.Width",
    x = "Sepal Length",
    y = "Sepal Width"
  ) +
  theme_minimal()

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

#câu 2

import pandas as pd
df = pd.read_csv("iris.csv") 
df_loc = df[df["Petal.Length"] > 4.0]
print(df_loc)
##      Sepal.Length  Sepal.Width  Petal.Length  Petal.Width     Species
## 50            7.0          3.2           4.7          1.4  versicolor
## 51            6.4          3.2           4.5          1.5  versicolor
## 52            6.9          3.1           4.9          1.5  versicolor
## 54            6.5          2.8           4.6          1.5  versicolor
## 55            5.7          2.8           4.5          1.3  versicolor
## ..            ...          ...           ...          ...         ...
## 145           6.7          3.0           5.2          2.3   virginica
## 146           6.3          2.5           5.0          1.9   virginica
## 147           6.5          3.0           5.2          2.0   virginica
## 148           6.2          3.4           5.4          2.3   virginica
## 149           5.9          3.0           5.1          1.8   virginica
## 
## [84 rows x 5 columns]
tong_sepal_length = df_loc["Sepal.Length"].sum()
print("Tong cot Sepal.Length (Petal.Length > 4.0):", tong_sepal_length)
## Tong cot Sepal.Length (Petal.Length > 4.0): 538.4