#BÀI HỌC SỐ 1 # CÁC THAO TÁC CƠ BẢN # 1. CÀI ĐẶT CÁC GÓI PHỔ THÔNG ( tidyverse)
install.packages("tidyverse")
#2. Gọi thư viện cần sử dụng
## 2.1 vẽ biểu đồ ggplot2
library(ggplot2)
##2.2 xử lý đầu vào và làm sạch chuyển đổi dữ liệu dplyr, tidyr, readr, readxl
library(dplyr)
library(tidyr)
library(readr)
library(readxl)
CO2
#3.1 xem dữ liệu riêng
View(CO2)
head(CO2)
str(CO2)
summary(CO2)
CO2 %>%
select(Type, conc)
CO2%>%
filter(conc >500)
## 1 đọc dữ liệu mẫu có sẵn mpg
mpg
##2 xem giới thiệu thông tin về dữ liệu mẫu này
?mpg
## 2 quan sát cấu trúc dữ liệu
head(mpg)
View(mpg)
## 3 đánh giá hồ sơ dữ liệu
summary(mpg)
## sau khi tóm tắt xong thì chúng ta thấy được các giá trị về số lượng của từng trường dữ liệu
## 4. Khai thác dữ liệu
### 4.1 Chọn một số cột
mpg$hwy
chon_mpg <- mpg %>%
select(hwy, cty)
head(chon_mpg)
### 4.2 Lọc một số dòng thỏa mãn điều kiện
loc_mpg<- mpg%>%
filter(hwy >20)
head(loc_mpg)
### 4.3 Chỉ chọn một số cột thỏa mãn điều kiện
mpg_locchon <- mpg%>%
filter(hwy >20) %>%
select(hwy, cty)
View(mpg_locchon)
## 5. vẽ biểu đồ với R mặc định
plot(mpg$displ, mpg$hwy)
##5. vẽ biểu đồ với thư viện ggplot2
ggplot(data = mpg) +
geom_point( mapping = aes(x= mpg$displ, y = mpg$hwy))
## 6. thêm màu sắc
ggplot(data = mpg) +
geom_point( mapping = aes(x= mpg$displ, y = mpg$hwy, color = mpg$class))
Titanic iris diamonds
hangxe <- mpg%>%
group_by(mpg$manufacturer)%>%
summarise(soluong = n())
hangxe
dongxe <- mpg%>%
group_by(mpg$model)%>%
summarise(soluong = n())
dongxe
```r
# 4.3 thống kê năm sản xuất
```r
namsx <-mpg%>%
group_by(mpg$year) %>%
summarise(soluong = n())
namsx
##4.1
dem_hx <- table(mpg$manufacturer)
dem_hx
dem_nam <-table(mpg$year)
dem_nam
hangxe1 <- mpg%>%
group_by(mpg$manufacturer)%>%
summarise(soluong =n(),
tonxang = min(hwy),
tietkiem= max(hwy),
tb_tieuthu= mean(hwy),
trungvu_tt = median(hwy)
)
hangxe1 <- mpg
loaixe <- mpg%>%
group_by(mpg$class)%>%
summarise(
soluong = n(),
ttmin= min(hwy),
ttmax = max(hwy),
tbtt= mean(hwy),
tvitt=median(hwy)
)
loaixe
View(loaixe)
hist(mpg$hwy)
ggplot(data = mpg)+
geom_histogram(mapping = aes(x= hwy))
ggplot(data = mpg)+
geom_histogram(bins = 10, mapping = aes(x= hwy))
ggplot(data = mpg)+
geom_bar(mapping = aes(x= class, y =n))
#———————–
library(readxl) # gọi file excel. Lưu ý tên file để địa chỉ chính xác Superstore <- read_excel(“3.SUPERSTORE.xlsx”, sheet = 1)
head(Superstore)
summary(Superstore)
#—Bộ dữ liệu thông tin thẻ tín dụng–
creditdata <- read_excel("creditdata.xlsx", sheet =1)
creditdata
View(creditdata)
credit_data <- creditdata
credit_data[ is.na (credit_data)] <- 0
View(credit_data)
names(credit_data) <-gsub(" ", "_", names(credit_data))
View(credit_data)
# Thống kê
theonhom1 <- credit_data%>%
group_by(credit_data$Term)%>%
summarise(soluong = n())
theonhom1
## phâm bổ theo nhóm
library(ggplot2)
ggplot(data = credit_data) +
geom_bar(mapping = aes(x= Term))
ggplot(data = credit_data) +
geom_bar(mapping = aes(x= Term)) +
labs( title = " Số lượng kh theo Nhóm",
x = " Nhóm",
y = " Số lượng")
ggplot(data = credit_data) +
geom_bar(mapping = aes(x= Home_Ownership)) +
labs( title = " Số lượng kh theo nhóm sở hữu nhà",
x = " Nhóm",
y = " Số lượng")
## cách thể hiện khác
ggplot(credit_data, aes(x = Home_Ownership)) +
geom_bar( fill = "blue")
ggplot(credit_data, aes(x = Home_Ownership, color = Term)) +
geom_bar( fill ="#1C2686")