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
library(ggplot2)
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
#Tính giá trị trung bình
bang_trung_binh <- iris %>%
group_by(Species) %>% # Nhóm dữ liệu theo loài hoa (Species)
summarise(
TB_Sepal_Length = mean(Sepal.Length),
TB_Petal_Length = mean(Petal.Length)
)
head(bang_trung_binh)
## # A tibble: 3 × 3
## Species TB_Sepal_Length TB_Petal_Length
## <fct> <dbl> <dbl>
## 1 setosa 5.01 1.46
## 2 versicolor 5.94 4.26
## 3 virginica 6.59 5.55
ggplot(data = iris, aes(x = Sepal.Width, y = Petal.Width, color = Species)) +
geom_point(size = 5) +
labs(
title = "Biểu đồ tương quan giữa Độ rộng đài hoa và cánh hoa",
x = "Sepal Width (do rong dai hoa)",
y = "Petal Width (do rong canh hoa)"
) +
theme_minimal()
#Câu 2
import pandas as pd
df = pd.read_csv('iris.csv')
# 2. Tính tổng theo nhóm Species (numeric_only=True để tự loại bỏ cột chữ)
result = df.groupby('Species').sum(numeric_only=True)
result.to_csv('total_values_by_species.csv')
result.tail()
## Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm
## Species
## Iris-setosa 1275 250.3 170.9 73.2 12.2
## Iris-versicolor 3775 296.8 138.5 213.0 66.3
## Iris-virginica 6275 329.4 148.7 277.6 101.3