Tải package xlsx và gọi dữ liệu file lên R với câu lệnh:
Đọc dữ liệu file xlsx với câu lệnh: read.xlsx()
library('xlsx') #gọi dữ liệu xlsx lên R
d <- read.xlsx(file = 'C:/Users/Admin/OneDrive/Documents/NNLT/afterlife.xlsx', sheetIndex = 1, header = T)
Bước 1: Tải gói package datasets với câu lệnh install.package()
library('datasets') #gọi package datasets sau khi đã tải xong
data(package = 'datasets') #xem trong gói package datasets có những bộ dữ liệu nào
Bước 2: Gọi bộ dữ liệu CO2 có sãn trong datasets
td <- CO2#gán bộ dữ liệu 'cars' vào a
Bước 3: Sử dụng bộ dữ liệu CO2:
is.data.frame(td) #kiểm tra xem a có phải là 1 data frame hay không
## [1] TRUE
length(td) #kiểm tra chiều dài của dữ liệu
## [1] 5
names(td) #tên biến của dữ liệu
## [1] "Plant" "Type" "Treatment" "conc" "uptake"
dim(td) #mô tả dữ liệu có bao nhiêu dòng và bao nhiêu cột
## [1] 84 5
library(skimr) #gọi package skimr
skim(td) #mô tả chi tiết dữ liệu
| Name | td |
| Number of rows | 84 |
| Number of columns | 5 |
| _______________________ | |
| Column type frequency: | |
| factor | 3 |
| numeric | 2 |
| ________________________ | |
| Group variables | None |
Variable type: factor
| skim_variable | n_missing | complete_rate | ordered | n_unique | top_counts |
|---|---|---|---|---|---|
| Plant | 0 | 1 | TRUE | 12 | Qn1: 7, Qn2: 7, Qn3: 7, Qc1: 7 |
| Type | 0 | 1 | FALSE | 2 | Que: 42, Mis: 42 |
| Treatment | 0 | 1 | FALSE | 2 | non: 42, chi: 42 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| conc | 0 | 1 | 435.00 | 295.92 | 95.0 | 175.0 | 350.0 | 675.00 | 1000.0 | ▇▂▂▂▂ |
| uptake | 0 | 1 | 27.21 | 10.81 | 7.7 | 17.9 | 28.3 | 37.12 | 45.5 | ▇▇▅▇▇ |
head(td,10) #đầu, hiện 10 dòng đầu tiên của dữ liệu
## Plant Type Treatment conc uptake
## 1 Qn1 Quebec nonchilled 95 16.0
## 2 Qn1 Quebec nonchilled 175 30.4
## 3 Qn1 Quebec nonchilled 250 34.8
## 4 Qn1 Quebec nonchilled 350 37.2
## 5 Qn1 Quebec nonchilled 500 35.3
## 6 Qn1 Quebec nonchilled 675 39.2
## 7 Qn1 Quebec nonchilled 1000 39.7
## 8 Qn2 Quebec nonchilled 95 13.6
## 9 Qn2 Quebec nonchilled 175 27.3
## 10 Qn2 Quebec nonchilled 250 37.1
tail(td,10) #đuôi, hiện 10 dòng cuối của dữ liệu
## Plant Type Treatment conc uptake
## 75 Mc2 Mississippi chilled 500 12.5
## 76 Mc2 Mississippi chilled 675 13.7
## 77 Mc2 Mississippi chilled 1000 14.4
## 78 Mc3 Mississippi chilled 95 10.6
## 79 Mc3 Mississippi chilled 175 18.0
## 80 Mc3 Mississippi chilled 250 17.9
## 81 Mc3 Mississippi chilled 350 17.9
## 82 Mc3 Mississippi chilled 500 17.9
## 83 Mc3 Mississippi chilled 675 18.9
## 84 Mc3 Mississippi chilled 1000 19.9
Đổi tên cho các biến để thuận tiện cho việc thao tác dữ liệu:
names(td) <- c('P', 'Ty', 'Tr', 'co', 'up')
Các thao tác rút trích dữ liệu:
td1 <- td[td$Tr =='chilled',]
str(td1)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame': 42 obs. of 5 variables:
## $ P : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 4 4 4 4 4 4 4 6 6 6 ...
## $ Ty: Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ...
## $ Tr: Factor w/ 2 levels "nonchilled","chilled": 2 2 2 2 2 2 2 2 2 2 ...
## $ co: num 95 175 250 350 500 675 1000 95 175 250 ...
## $ up: num 14.2 24.1 30.3 34.6 32.5 35.4 38.7 9.3 27.3 35 ...
## - attr(*, "formula")=Class 'formula' language uptake ~ conc | Plant
## .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
## - attr(*, "outer")=Class 'formula' language ~Treatment * Type
## .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
## - attr(*, "labels")=List of 2
## ..$ x: chr "Ambient carbon dioxide concentration"
## ..$ y: chr "CO2 uptake rate"
## - attr(*, "units")=List of 2
## ..$ x: chr "(uL/L)"
## ..$ y: chr "(umol/m^2 s)"
td2 <- td[td$Tr =='chilled' & td$Ty =='Quebec', ]
td3 <- td[td$co>600 ,]
td4 <- td[td$Tr != 'chilled',]
Ví dụ:
tt <- trees
tich <- tt$Girth*tt$Height*tt$Volume
tt<- cbind(tt,tich)
l <- log(tich)
tt <- cbind(tt,l)
Ngoài ra chúng ta có thể sử dung package Tidyverse ( một package quan trọng của R, nó bao gồm nhiều package con ) để thay thế các thao tác trên bằng các lệnh sau:
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
tt <- trees
tt <- tt %>% mutate(tich = Girth*Height*Volume)
tt <- tt %>% mutate(l = log(tich))
World Bank lưu trữ rất nhiều thông tin kinh tế vĩ mô của nhiều nước trên thế giới và hoàn toàn cho phép người dùng truy cập và sử dụng dữ liệu.