2024-01-22
Data manipulation involves modifying data to make it easier to read and to be more organized. We manipulate data for analysis and visualization.
It is also used with the term ‘data exploration’ which involves organizing data using available sets of variables.
At times, the data collection process done by machines involves a lot of errors and inaccuracies in reading. Data manipulation is also used to remove these inaccuracies and make data more accurate and precise.
WB 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.
Đây là bộ dữ liệu có sẵn trong package datasets.
x <- c(1, 2, 0 / 0, 3, NA, 4, 0 / 0)
x[is.na(x)] <- mean(x,na.rm = T)
library(WDI)
d <- WDI(indicator = 'FI.RES.TOTL.MO', country = c('VNM'))
tmp <- tmp %>% select(year,FI.RES.TOTL.MO)
tmp <- tmp %>% rename(dutru = FI.RES.TOTL.MO)
tmp <- d
tmp$dutru[is.na(tmp$dutru)] <- mean(tmp$dutru, na.rm = TRUE)
tmp <- d
tmp <- tmp[complete.cases(tmp),]
tmp <- na.omit(tmp)
Bộ dữ liệu iris
Đổi tên cho các biến để thuận tiện cho việc thao tác trên dữ liệu.
Thực hiện thao tác rút trích dữ liệu
Bộ dữ liệu iris cũng là một bộ dữ liệu có sẵn trong package datasets và trong bộ dữ liệu này có cả dữ liệu định tính và dữ liệu định lượng.
tidyverse là một package quan trọng của R, nó bao gồm nhiều package con. Trong phần này chúng ta chỉ nói về việc rút trích dữ liệu.
Các hàm toán học trong R: abs,sqrt,…
Xóa cột
Xóa hàng:
tmp <- iris
names(tmp) <- c('SL','SW','PL','PW','S')
tmp$S.Coded <- ifelse(tmp$S == 'setosa','setosa','Not setosa')
tmp$S.Coded1 <- recode(tmp$S,setosa = 'Loại 1', versicolor = 'Loại 2')
tmp$SL.Coded <- ifelse(tmp$SL >= 5,'Đạt', 'Không đạt')
tmp$SL.Coded1 <- ifelse(tmp$SL >= 5 & tmp$SL <= 6, 'Nhận', 'Loại')
tmp$SL.Coded2 <- case_when(tmp$SL < 5 ~ 'Quá nhỏ', tmp$SL >= 5 & tmp$SL <= 6.5 ~ 'OK', tmp$SL >6.5 ~ 'Quá lớn')
tmp$SL.Coded2 <- cut(tmp$SL,3,labels = c('Loại 1','Loại 2','Loại 3'))
Lập bảng tần số cho 1 biến
Lập bảng tần số cho 2 biến
Hoặc chúng ta có thể thực hiện bằng 1 cách khác như sau:
Lập biểu đồ nhánh và lá
tmp <- diamonds
moc <- tmp %>% group_by(color) %>% summarise(mean_of_carat = mean(carat))
moc <- tmp %>% group_by(color) %>% summarise(n = n(),mean_of_carat = mean(carat))
medoc <- tmp %>% group_by(color) %>% summarise(med_of_carat = median(carat))
moc1 <- tmp %>% group_by(cut) %>% summarise(mean_of_carat = mean(carat))
moc2 <- tmp %>% group_by(color,cut) %>% summarise(n = n(),mean_of_carat = mean(carat),.groups = 'drop')
Đây là 1 wide table
Comp Type Equity Tax
1 A Sole proprietorship 10.1 0.54
2 B Partnership 8.0 0.22
3 C Corporation 5.2 0.08
4 D Limited liability company 1.1 0.02
5 E Cooperative 115.0 5.80
Đây là long table
Comp Đặc điểm Ghi chú
1 A Type Sole proprietorship
2 B Type Partnership
3 C Type Corporation
4 D Type Limited liability company
5 E Type Cooperative
6 A Equity 10.1
7 B Equity 8
8 C Equity 5.2
9 D Equity 1.1
10 E Equity 115
11 A Tax 0.54
12 B Tax 0.22
13 C Tax 0.08
14 D Tax 0.02
15 E Tax 5.8
Đây là 1 wide table
Đây là long table
Comp Đặc điểm Ghi chú
1 A Type Sole proprietorship
2 B Type Partnership
3 C Type Corporation
4 D Type Limited liability company
5 E Type Cooperative
6 A Equity 10.1
7 B Equity 8
8 C Equity 5.2
9 D Equity 1.1
10 E Equity 115
11 A Tax 0.54
12 B Tax 0.22
13 C Tax 0.08
14 D Tax 0.02
15 E Tax 5.8
Chuẩn bị dữ liệu:
library(tidyverse)
library(googledrive)
pa <- 'https://drive.google.com/file/d/1Jc14iMyezkp0IARQdEPe03HbHh6NCiZA/view?usp=sharing'
id <- as_id(pa)
pa <- paste0('https://docs.google.com/uc?id=',id,'&export=download')
d <- read.csv(pa, header = T)
tmp <- d %>% filter(Country.name %in% c('Vietnam','Thailand','Malaysia', 'cambodia', 'Laos'))
tmp <- tmp %>% select(Country.name,Population,Year)
tmp <- tmp %>% rename(country = Country.name ,pop = Population, year = Year)
tmp
Chuyển dữ liệu từ long sang wide
bind_cols(df_1, df_2) # Appending a table to the right side (horizontal) of another
bind_rows(df_1, df_2) # Appending a table to the bottom (vertical) of another
union(df_1, df_2) # Combining rows that exist in both tables and dropping duplicates
union_all(df_1, df_2) # Combining all rows that exist in both table.
intersect(df_1, df_2) # Finding identical columns in both tables
setdiff(df_1, df_2) # Finding rows that don’t exist in another table
merge(df_1, df_2, join_field, join_type) #We can merge two data frames by a given field
df %>% mutate(new_col = operation(other_cols)) # Add new columns on top of old ones
df %>% transmute(new_col = operation(other_cols)) # Add new columns and discard old ones
df %>% mutate_at(vars, funs) # Modify several columns in-place
df %>% mutate_all(funs) # Modify all columns in-place
df %>% mutate_if(condition, funs) # Modify columns fitting a specific condition
df %>% unite(new_merged_col, old_cols_list) # Unite columns
df %>% separate(col_to_separate, new_cols_list) # Separate columns
inner_join(x, y) # keeps observations appearing in both tables.
left_join(x, y) # keeps all observations in x and only adds matches from y.
right_join(x, y) # keeps all observations in y and only adds matches from x.
full_join(x, y) # keeps all observations in x and y; if there’s no match, it returns NAs.