BAI HOC SO 1

Cac thao tac co ban

1 Cai dat goi phothong

install.packages(“tidyverse”)

2 goi thu vien can su dung

2.1 ve bieu do 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)

3 Đọc dữ liệu

CO2

3.1 Xem dữ liệu

View(CO2)

3.2 Xem nội dung trực tiếp

head(CO2)

4 Đánh giá chất lượng bộ dữ liệu

summary(CO2) str(CO2)

5 Khai thac du lieu

5.1 Chọn các trường can su dung

CO2 %>% select(Type, conc)

5.2 Loc theo dieu kien

CO2 %>% filter(conc>500) # THƯC HANH——————————

mpg

?mpg head(mpg)

summary(mpg)

mpg$hwy

;Khai bao bang tam

mpg_loc <- mpg %>% filter(hwy>20) ; mpg_loc ; mpg_loc1 <- mpg%>% filter(hwy>20) %>% select(hwy, cty)

head(mpg_loc1) View(mpg_loc1) # Ve bieu voi R mac dinh plot(mpg\(displ, mpg\)hwy)

Ve bieu do voi thu vien ggplot2

ggplot(data = mpg) + geom_point(mapping = aes(x = mpg\(displ, y = mpg\)hwy, colour = mpg$class)) ; ————————– # 1 Quan sat du lieu # 2 Danh gia du lieu # 3 Khai thac du lieu # pha du lieu

4.1 Thong ke so hang xe voi group_by

hangxe <- mpg %>% group_by(mpg\(manufacturer) %>% summarise(soluong = n()) ; head(hangxe) ; ## 4.2. Thong ke dong xe voi group_by dongxe <- mpg %>% group_by(mpg\)model) %>% summarise(soluong = n()) ; head(dongxe) ; dongxe ; ## 4.3. Thong ke nam san xuat voi group_by 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 # 5 Thong ke mo ta su viec

hangxe <- mpg %>% group_by(mpg\(manufacturer) %>% summarise(soluong = n(), tonxang = min(hwy), tietkiem = max(hwy), tb_tieuthu = mean(hwy), trungvi_tt = median(hwy) ) hangxe ; loaixe <- mpg %>% group_by(mpg\)class) %>% summarise(soluong = n(), tonxang = min(hwy), tietkiem = max(hwy), tb_tieuthu = mean(hwy), trungvi_tt = median(hwy) ) loaixe

; # 6. Bieu do hieu suat tieu hao nguyen lieu hist(mpg$hwy)

ggplot(data = mpg) + geom_histogram(mapping = aes(x =hwy) ) loaixe # 7. Bieu do so sanh -> check lai ggplot(data = loaixe) + geom_bar(mapping = aes(x= class, y = n), start = “identify”) ; ggplot(data = loaixe) + geom_bar(mapping = aes(x= class, y = soluong)) ; loaixe

##————–Phan 2: Su dung du lieu tuy bien de phan tich #1 Lay du lieu tu file excel ##1.1 Goi file excel: can phan set dia chi thu muc chinh xac: Session >> Set Working Directory >> tro toi thu muc #hoac de ca duong dan de load file Superstore <- read_excel(“3.SUPERSTORE.xlsx”,sheet = 1) ; ## 1.2 Quan sat file View(Superstore) head(Superstore) ; ## 1.3 Danh gia chat luong du lieu summary(Superstore) ; #————–Bo du lieu thong tin the tin dung # 1 Lay du lieu vao R creditdata <- read_excel(“creditdata.xlsx”) ; creditdata # 2.1 Xu ly lam sach du lieu voi loi NA laf chuyen het NA ve 0 credit_data <- creditdata credit_data[is.na(credit_data)] <- 0 ; view(credit_data) # 2.2 Xu ly lam sach thay the toan bo dau cach trong tren tieu de thanh names(credit_data) <- gsub(” “,””, names(credit_data)) ; head(credit_data) ; # Thong ke theonhom <- credit_data %>% group_by(credit_data$Term)%>% summarise(soluong = n() ) theonhom ; # Phan Bo theo nhom ky han ggplot(data = credit_data)+ geom_bar(mapping = aes(x = Term)) + labs(titel = “So luong khach hang theo nhom”, x = “Nhom”, y = “So luong”) ; # Phan bo theo nhom so huu ggplot(data = credit_data)+ geom_bar(mapping = aes(x = Home_Ownership)) + labs(titel = “So luong khach hang theo nhom so huu nha”, x = “Nhom”, y = “So luong”) ; ggplot(credit_data,aes(x = Home_Ownership#,color = Term ))+ geom_bar(fill = “blue”) ; ggplot(credit_data,aes(x = Home_Ownership,color = Term ))+ geom_bar(fill = “orange”) ; ggplot(credit_data,aes(x = Home_Ownership,color = Term ))+ geom_bar(fill = “#1C2686”) ; ggplot(credit_data) + geom_bar(aes(x = Home_Ownership), fill = “blue”)

; head(credit_data) theonhom <- credit_data %>% select(credit_data$Loan_ID)