install.packages(“tidyverse”)
library(ggplot2)
library(dplyr) library(tidyr) library(readr) library(readxl)
CO2
View(CO2)
head(CO2)
summary(CO2) str(CO2)
CO2 %>% select(Type, conc)
CO2 %>% filter(conc>500) # THƯC HANH——————————
mpg
?mpg head(mpg)
summary(mpg)
mpg$hwy
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)
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
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
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)