
R語言數(shù)據(jù)處理包dplyr、tidyr筆記
dplyr包是Hadley Wickham的新作,主要用于數(shù)據(jù)清洗和整理,該包專注dataframe數(shù)據(jù)格式,從而大幅提高了數(shù)據(jù)處理速度,并且提供了與其它數(shù)據(jù)庫的接口;tidyr包的作者是Hadley Wickham, 該包用于“tidy”你的數(shù)據(jù),這個包常跟dplyr結合使用。
本文將介紹dplyr包的下述五個函數(shù)用法:
篩選: filter()
排列: arrange()
選擇: select()
變形: mutate()
匯總: summarise()
分組: group_by()
以及tidyr包的下述四個函數(shù)用法:
gather—寬數(shù)據(jù)轉為長數(shù)據(jù);
spread—長數(shù)據(jù)轉為寬數(shù)據(jù);
unit—多列合并為一列;
separate—將一列分離為多列;
dplyr、tidyr包安裝及載入
install.packages("dplyr")
install.packages("tidyr")
library(dplyr)
library(tidyr)
使用datasets包中的mtcars數(shù)據(jù)集做演示,首先將過長的數(shù)據(jù)整理成友好的tbl_df數(shù)據(jù):
mtcars_df = tbl_df(mtcars)
dplyr包基本操作
1.1 篩選: filter()
按給定的邏輯判斷篩選出符合要求的子數(shù)據(jù)集
filter(mtcars_df,mpg==21,hp==110)
# A tibble: 2 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21 6 160 110 3.9 2.620 16.46 0 1 4 4
2 21 6 160 110 3.9 2.875 17.02 0 1 4 4
1.2 排列: arrange()
按給定的列名依次對行進行排序:
arrange(mtcars_df, disp) #可對列名加 desc(disp) 進行倒序
# A tibble: 32 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
2 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
3 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
4 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
5 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
6 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
7 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
8 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
9 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
10 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
# ... with 22 more rows
1.3 選擇: select()
用列名作參數(shù)來選擇子數(shù)據(jù)集:
select(mtcars_df, disp:wt)
# A tibble: 32 x 4
disp hp drat wt
* <dbl> <dbl> <dbl> <dbl>
1 160.0 110 3.90 2.620
2 160.0 110 3.90 2.875
3 108.0 93 3.85 2.320
4 258.0 110 3.08 3.215
5 360.0 175 3.15 3.440
6 225.0 105 2.76 3.460
7 360.0 245 3.21 3.570
8 146.7 62 3.69 3.190
9 140.8 95 3.92 3.150
10 167.6 123 3.92 3.440
# ... with 22 more rows
1.4 變形: mutate()
對已有列進行數(shù)據(jù)運算并添加為新列:
mutate(mtcars_df,
NO = 1:dim(mtcars_df)[1])
# A tibble: 32 x 12
mpg cyl disp hp drat wt qsec vs am gear carb NO
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 1
2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 2
3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 3
4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 4
5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 5
6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 6
7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 7
8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 8
9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 9
10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 10
# ... with 22 more rows
1.5 匯總: summarise()
對數(shù)據(jù)框調用其它函數(shù)進行匯總操作, 返回一維的結果:
summarise(mtcars_df,
mdisp = mean(disp, na.rm = TRUE))
# A tibble: 1 x 1
mdisp
<dbl>
1 230.7219
1.6 分組: group_by()
當對數(shù)據(jù)集通過group_by()添加了分組信息后,mutate(),arrange() 和 summarise() 函數(shù)會自動對這些 tbl 類數(shù)據(jù)執(zhí)行分組操作。
cars <- group_by(mtcars_df, cyl)
countcars <- summarise(cars, count = n()) # count = n()用來計算次數(shù)
# A tibble: 3 x 2
cyl count
<dbl> <int>
1 4 11
2 6 7
3 8 14
tidyr包基本操作
2.1 寬轉長:gather()
使用gather()函數(shù)實現(xiàn)寬表轉長表,語法如下:
gather(data, key, value, …, na.rm = FALSE, convert = FALSE)
data:需要被轉換的寬形表
key:將原數(shù)據(jù)框中的所有列賦給一個新變量key
value:將原數(shù)據(jù)框中的所有值賦給一個新變量value
…:可以指定哪些列聚到同一列中
na.rm:是否刪除缺失值
widedata <- data.frame(person=c('Alex','Bob','Cathy'),grade=c(2,3,4),score=c(78,89,88))
widedata
person grade score
1 Alex 2 78
2 Bob 3 89
3 Cathy 4 88
longdata <- gather(widedata, variable, value,-person)
longdata
person variable value
1 Alex grade 2
2 Bob grade 3
3 Cathy grade 4
4 Alex score 78
5 Bob score 89
6 Cathy score 88
2.2 長轉寬:spread()
有時,為了滿足建模或繪圖的要求,往往需要將長形表轉換為寬形表,或將寬形表變?yōu)殚L形表。如何實現(xiàn)這兩種數(shù)據(jù)表類型的轉換。使用spread()函數(shù)實現(xiàn)長表轉寬表,語法如下:
spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE)
data:為需要轉換的長形表
key:需要將變量值拓展為字段的變量
value:需要分散的值
fill:對于缺失值,可將fill的值賦值給被轉型后的缺失值
mtcarsSpread <- mtcarsNew %>% spread(attribute, value)
head(mtcarsSpread)
car am carb cyl disp drat gear hp mpg qsec vs wt
1 AMC Javelin 0 2 8 304 3.15 3 150 15.2 17.30 0 3.435
2 Cadillac Fleetwood 0 4 8 472 2.93 3 205 10.4 17.98 0 5.250
3 Camaro Z28 0 4 8 350 3.73 3 245 13.3 15.41 0 3.840
4 Chrysler Imperial 0 4 8 440 3.23 3 230 14.7 17.42 0 5.345
5 Datsun 710 1 1 4 108 3.85 4 93 22.8 18.61 1 2.320
6 Dodge Challenger 0 2 8 318 2.76 3 150 15.5 16.87 0 3.520
2.3 合并:unit()
unite的調用格式如下:
unite(data, col, …, sep = “_”, remove = TRUE)
data:為數(shù)據(jù)框
col:被組合的新列名稱
…:指定哪些列需要被組合
sep:組合列之間的連接符,默認為下劃線
remove:是否刪除被組合的列
wideunite<-unite(widedata, information, person, grade, score, sep= "-")
wideunite
information
1 Alex-2-78
2 Bob-3-89
3 Cathy-4-88
2.4 拆分:separate()
separate()函數(shù)可將一列拆分為多列,一般可用于日志數(shù)據(jù)或日期時間型數(shù)據(jù)的拆分,語法如下:
separate(data, col, into, sep = “[^[:alnum:]]+”, remove = TRUE,
convert = FALSE, extra = “warn”, fill = “warn”, …)
data:為數(shù)據(jù)框
col:需要被拆分的列
into:新建的列名,為字符串向量
sep:被拆分列的分隔符
remove:是否刪除被分割的列
widesep <- separate(wideunite, information,c("person","grade","score"), sep = "-")
widesep
person grade score
1 Alex 2 78
2 Bob 3 89
3 Cathy 4 88
可見separate()函數(shù)和unite()函數(shù)的功能相反。
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
SQL Server 中 CONVERT 函數(shù)的日期轉換:從基礎用法到實戰(zhàn)優(yōu)化 在 SQL Server 的數(shù)據(jù)處理中,日期格式轉換是高頻需求 —— 無論 ...
2025-09-18MySQL 大表拆分與關聯(lián)查詢效率:打破 “拆分必慢” 的認知誤區(qū) 在 MySQL 數(shù)據(jù)庫管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結構數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結構數(shù)據(jù)(如數(shù)據(jù)庫表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預期算子的內涵、作用與應用解析 動態(tài)隨機一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結構數(shù)據(jù)特征價值的專業(yè)核心 表結構數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲的結構化數(shù)據(jù),如數(shù)據(jù)庫表、Excel 表、 ...
2025-09-17Excel 導入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實戰(zhàn)應用 在用 Python(如 pandas 庫)處理 Excel 數(shù)據(jù)時,“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗與 t 檢驗:差異、適用場景與實踐應用 在數(shù)據(jù)分析與統(tǒng)計學領域,假設檢驗是驗證研究假設、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結構數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結構數(shù)據(jù)(以 “行 - 列” 存儲的結構化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計劃中 rows 數(shù)量的準確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調優(yōu)中,EXPLAIN執(zhí)行計劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對象的 text 與 content:區(qū)別、場景與實踐指南 在 Python 進行 HTTP 網絡請求開發(fā)時(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結構數(shù)據(jù)價值的核心操盤手 表格結構數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫表)是企業(yè)最基礎、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請求工具對比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請求(如接口調用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長浮點數(shù)據(jù)的科學計數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長浮點數(shù)據(jù)時的科學計數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務數(shù)據(jù)分析步驟的落地者與價值優(yōu)化者 業(yè)務數(shù)據(jù)分析是企業(yè)解決日常運營問題、提升執(zhí)行效率的核心手段,其價值 ...
2025-09-12用 SQL 驗證業(yè)務邏輯:從規(guī)則拆解到數(shù)據(jù)把關的實戰(zhàn)指南 在業(yè)務系統(tǒng)落地過程中,“業(yè)務邏輯” 是連接 “需求設計” 與 “用戶體驗 ...
2025-09-11塔吉特百貨孕婦營銷案例:數(shù)據(jù)驅動下的精準零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當下,精準營銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務數(shù)據(jù)分析:概念辨析與協(xié)同價值 在數(shù)據(jù)驅動決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實踐到業(yè)務價值挖掘 在數(shù)據(jù)分析場景中,聚類分析作為 “無監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計模型的核心目的:從數(shù)據(jù)解讀到決策支撐的價值導向 統(tǒng)計模型作為數(shù)據(jù)分析的核心工具,并非簡單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10