R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

data() View(CO2)

View(AirPassengers)

#bài học cơ bản #bài 1: thao tác co ban #cài đặt tidyverse

install.packages(“tidyverse”) #Gọi thư viện cần sử dụng ggplot2 library(ggplot2)

#xử lý dữ liệu làm sạch dplyr, tidyr, readr

library(dplyr) library(tidyr) library(readr) library(readxl) #view tổng CO2

#View chi tiết View(co2) # đánh giá chất lượng dữ liệu str(CO2) summary(CO2) # CHỌN CÁC TRƯỜNG CẦN LẤY CO2 %>% select(Type, conc)

###lọc theo điều kiện CO2 %>% filter(conc > 500)

##thực hành library(tidyverse) mpg ##xem gthieu về thông tin dl mẫu ?mpg ##quan sát cấu trúc dl -> cho biết kiểu dữ liệu tương ứng head(mpg) ## đánh giá hồ sơ dữ liệu

summary(mpg)

##sau khi tóm tắt xong chúng ta thấy được các giá trị về số lượng ##chọn 1 số cột

mpg$hwy

##lọc theo điều kiện mpg%>% filter(hwy > 20)

##chọn 1 số cột thoả mãn điều kiện mpg_loc <- mpg%>% select(mpg\(hwy) filter(mpg\)hwy > 20)

head(mpg_loc)

##vẽ biểu đồ với R mặc định plot(mpg\(displ, mpg\)hwy)

vẽ biểu đồ ggplot2

ggplot(data = mpg) + geom_point(mapping = aes(x = mpg\(displ, y = mpg\)hwy))

##có màu ggplot(data = mpg) + geom_point(mapping = aes(x = mpg\(displ, y = mpg\)hwy, colour = mpg$class))

View(mpg) Titanic iris diamonds —

#thống kê bộ hãng xe hangxe <- mpg %>% group_by(mpg\(manufacturer)%>% summarise(soluong = n()) hangxe #thong ke dong xe dongxe <- mpg %>% group_by(mpg\)model)%>% summarise(soluong = n())

dongxe

nam san xuat

namsanxuat <- mpg %>% group_by(mpg$year)%>% summarise(soluong = n())

namsanxuat ##ham dem so luong dem_hx <- table(mpg$manufacturer) dem_hx

dem_mau <-table(mpg\(model) dem_mau #thong ke mo ta su viec hangxe <- mpg %>% group_by(mpg\)manufacturer)%>% summarise(soluong = n(), tonxang = min(hwy), tietkiem = max(hwy), tbinh = mean(hwy), trungvi = median(hwy) ) hangxe

##Bieu do hist(mpg$hwy)

ggplot(data = mpg)+ geom_histogram(mapping = aes(x = hwy))

ggplot(data = mpg)+ geom_bar(mapping = aes (x = class, y = hwy))

ggplot(data = mpg)+ geom_histogram(bins =10, mapping = aes (x = hwy))

#bieu do so sanh ggplot(data = mpg) + geom_bar(mapping = aes(x = class))

##DUNG DU LIEU DE PHAN TICH # 1. LAY DU LIEU TU FILE EXCEL library(readxl)

superstore <- read_excel(“3.SUPERSTORE.xlsx”, sheet = 1) View(superstore)

summary(superstore)

creditdata <- read_excel(“creditdata.xlsx”, sheet = 1)

View(creditdata) ## xu ly NA ##lay danh sach NA credit_data <- creditdata credit_data[is.na (creditdata)] <- 0

View(credit_data)

##thay the toan bo ” ” tren thanh tieu de bang _

names(credit_data) <-gsub(” “,”_“, names(credit_data))

View(credit_data)

#thong ke theonhom <- credit_data%>% group_by(credit_data$Term)%>% summarise(soluong = n()) View(theonhom)

##bieu do ggplot(data = credit_data) + geom_bar(mapping = aes(x=Term)) + labs( title = “So luong KH theo nhom”, x = “Nhom”, y = “So luong”)

ggplot(credit_data, aes(x = Home_Ownership, colour = Term)) + geom_bar(fill = “green”)