---
title: Bài tập làm sạch và sắp xếp dữ liệu
author: Duc Nguyen | Chuyên đào tạo kỹ năng R | `www.tuhocr.com`
date: Apr 26, 2023
output:
  html_document:
    highlight: pygments
---

### Đề bài

**Bạn có 1 file [`crop_full.rds`](https://studyr.netlify.app/faostat/data_raw/crop_production_all_data.rds) (13.9 MB) chứa dữ liệu về nông nghiệp của FAO, gồm 3.7 triệu dòng. Bạn sử dụng package `FAOSTAT` để download về nhé. Cách thức download file này được mình trình bày ở BÀI 2: CÁCH TẢI DATASET TỪ FAOSTAT trong playlist [HỌC R SIÊU NHANH](https://www.youtube.com/playlist?list=PLCKFKYBfLgPV07MhYMoOMlazOSS7KwKuA).**

**Cấu trúc file này như sau: [crop_full.html](https://studyr.netlify.app/faostat/crop_full.html)**

**Trong file này thì cột `item` là tên các loại nông sản. Bạn hãy tìm cách trích xuất, làm sạch dữ liệu, sắp xếp làm sao đó để tách ra thành các bảng dữ liệu con tương ứng từng loại nông sản nhé.**

### Cách thực hiện

**Bước 1: Bạn xây dựng function `extract_item` để subset dữ liệu.**

```{r, message=FALSE, warning=FALSE}
crop_full <- readRDS("data_raw/crop_production_all_data.rds") ## Import dataset chung
dim(crop_full) ## Kiểm tra số lượng hàng và cột dataset.
```

<style>
div.yellow pre.r { background-color:yellow; }
</style>

<div class = "yellow">
```{r, message=FALSE, warning=FALSE}
source(file = "extract_item.R") ## Source function `extract_item` để giúp subset theo loại nông sản
```
</div>

**Ví dụ tách ra dataset cho dữ liệu lúa gạo, mã code `item == "Rice"`**

```{r, message=FALSE, warning=FALSE}
rice_country <- extract_item(input_item = "Rice",
                             input_rds = "data_raw/crop_production_all_data.rds",
                             input_region = "data_raw/FAOSTAT_data_3-21-2023.csv")
dim(rice_country)
```

**Bước 2: Bạn đưa dataset sau khi subset đó vào code render table HTML để có file cần tìm**.

```{r, message=FALSE, warning=FALSE, eval=FALSE}
library(kableExtra)

cbind(" " = 1:7174, rice_country) %>%
    kbl(escape = FALSE, caption = "<center><strong><span style='color: blue'>Dataset [item == 'Rice'] từ FAOSTAT gồm 7174 dòng và 7 cột | Minh họa: tuhocr.com</span></strong></center>",
        format = "html") %>%
    kable_styling(bootstrap_options = c("striped", 
                                        "hover", 
                                        "condensed", 
                                        "bordered", 
                                        "responsive")) %>%
    kable_classic(full_width = FALSE, html_font = "arial") %>% 
    add_header_above(c("Dataset này đã được làm sạch, thể hiện tình hình sản xuất lúa gạo ở 128 quốc gia và vùng lãnh thổ trên thế giới từ 1961 đến 2021. \nSắp xếp theo thứ tự từ lớn đến nhỏ tính theo sản lượng thu hoạch. Đơn vị: production (tấn), area_harvested (ha), yield (tấn/ha)." = 7), escape = TRUE, bold = TRUE, color = "darkgreen", background = "yellow")-> output

save_kable(output, file = "rice_country.html")
```

**1. Ta có kết quả [rice_country.html](https://studyr.netlify.app/faostat/rice_country.html)**

**2. Tương tự, các bạn có dataset của cà phê `item == "Coffee, green"` ở file [coffee_country.html](https://studyr.netlify.app/faostat/coffee_country.html)**

**3. Hồ tiêu `item == "Pepper (Piper spp.), raw"` ở file [piper_country.html](https://studyr.netlify.app/faostat/piper_country.html)**

**4. Ớt (khô) `item == "Chillies and peppers, dry (Capsicum spp., Pimenta spp.), raw"` ở file [chilly_dry_country.html](https://studyr.netlify.app/faostat/chilly_dry_country.html)**

**5. Trái dừa khô `item == "Coconuts, in shell"` ở file [coconut_country.html](https://studyr.netlify.app/faostat/coconut_country.html)**

### **Đáp án**

<mark>**Bài tập data cleaning and data wrangling ở trên được mình hướng dẫn cụ thể qua series [HỌC R SIÊU NHANH](https://www.youtube.com/playlist?list=PLCKFKYBfLgPV07MhYMoOMlazOSS7KwKuA), bao gồm source code và cách triển khai chi tiết trong phần tài khoản học viên.**</mark> 

<span style="color: green">**Để học R bài bản từ A đến Z, thân mời Bạn tham gia khóa học [HDSD R ĐỂ XỬ LÝ DỮ LIỆU](https://www.tuhocr.com/r-courses/coding-in-r) trang bị nền tảng vững chắc về R nhằm tự tay làm các câu chuyện dữ liệu của riêng mình!**</span>

<span style="color: blue">**ĐĂNG KÝ HỌC VIÊN: [https://www.tuhocr.com/register](https://www.tuhocr.com/register)**</span>

<img src="E:/tuhocr/tuhocr.png"  width=20% height=20%>




















