數(shù)據(jù)處理 | R-tidyr包

介紹tidyr包中五個基本函數(shù)的簡單用法:長轉(zhuǎn)寬,寬轉(zhuǎn)長,合并,分割,NA簡單填充。


長數(shù)據(jù)就是一個觀測對象可由多行組成,而寬數(shù)據(jù)則是一個觀測僅由一行組成。


#載入所需的R包

library(dplyr)

library(tidyr)


#測試數(shù)據(jù)集

widedata <- data.frame(person=c('A','B','C'),grade=c(5,6,4),score=c(89,98,90))

widedata


? person grade score

1 ? ? ?A ? ? 5 ? ?89

2 ? ? ?B ? ? 6 ? ?98

3 ? ? ?C ? ? 4 ? ?90


一 寬數(shù)據(jù)轉(zhuǎn)為長數(shù)據(jù)


gather(): 類似于reshape2包中的melt()函數(shù);

gather(data, key, value, … , na.rm = FALSE, convert = FALSE):

data:需要被轉(zhuǎn)換的寬形表

key:將原數(shù)據(jù)框中的所有列賦給一個新變量key

value:將原數(shù)據(jù)框中的所有值賦給一個新變量value

...:可以指定哪些列聚到一列中

na.rm:是否刪除缺失值


將示例數(shù)據(jù)集轉(zhuǎn)成長數(shù)據(jù):

longdata <- gather(widedata, variable, value)

longdata


?variable value

1 ? person ? ? A

2 ? person ? ? B

3 ? person ? ? C

4 ? ?grade ? ? 5

5 ? ?grade ? ? 6

6 ? ?grade ? ? 4

7 ? ?score ? ?89

8 ? ?score ? ?98

9 ? ?score ? ?90


只把制定變量從寬數(shù)據(jù)變成長數(shù)據(jù)的功能,person不變成長數(shù)據(jù)

gather(widedata, variable, value, -person)


? person variable value

1 ? ? ?A ? ?grade ? ? 5

2 ? ? ?B ? ?grade ? ? 6

3 ? ? ?C ? ?grade ? ? 4

4 ? ? ?A ? ?score ? ?89

5 ? ? ?B ? ?score ? ?98

6 ? ? ?C ? ?score ? ?90


gather()函數(shù)比reshape2包中melt()函數(shù)的優(yōu)勢: 它可以只gather若干列而其他列保持不變:


age <- c(20, 21, 22)

wide <- data.frame(widedata, age)

wide


? person grade score age

1 ? ? ?A ? ? 5 ? ?89 ?20

2 ? ? ?B ? ? 6 ? ?98 ?21

3 ? ? ?C ? ? 4 ? ?90 ?22


先對widedata增加一列 age. 整合兩個變量之間的若干列, 而保持其他列不變:


long <- gather(wide, variable, value, grade:age)

long


? person variable value

1 ? ? ?A ? ?grade ? ? 5

2 ? ? ?B ? ?grade ? ? 6

3 ? ? ?C ? ?grade ? ? 4

4 ? ? ?A ? ?score ? ?89

5 ? ? ?B ? ?score ? ?98

6 ? ? ?C ? ?score ? ?90

7 ? ? ?A ? ? ?age ? ?20

8 ? ? ?B ? ? ?age ? ?21

9 ? ? ?C ? ? ?age ? ?22



二 長數(shù)據(jù)轉(zhuǎn)為寬數(shù)據(jù)


spread():類似于reshape2包中的cast()函數(shù);

spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE)

data:為需要轉(zhuǎn)換的長形表

key:需要將變量值拓展為字段的變量

value:需要分散的值

fill:對于缺失值,可將fill的值賦值給被轉(zhuǎn)型后的缺失值


將長數(shù)據(jù)轉(zhuǎn)成寬數(shù)據(jù):

wide <- spread(long, variable, value)

wide


? person age grade score

1 ? ? ?A ?20 ? ? 5 ? ?89

2 ? ? ?B ?21 ? ? 6 ? ?98

3 ? ? ?C ?22 ? ? 4 ? ?90


這實(shí)際將原來gather后的結(jié)果還原為gather前, 但各列的相互位置稍有調(diào)整.


三 多列合并為一列


unite(data, col, … , sep = " ")

data::表示數(shù)據(jù)框,

col:表示合并后的列名稱,

… :表示需要合并的若干變量,

sep: = " "用于指定分隔符,

remove:是否刪除被組合的列


把widedata中的person,grade, score三個變量合成一個變量information, 并變成"person-grade-score"的格式


wideunite<-unite(widedata, information, person, grade, score, sep= "-")

wideunite


? information

1 ? ? ?A-5-89

2 ? ? ?B-6-98

3 ? ? ?C-4-90


四 一列分離為多列.


separate(data, col, into, sep = " ")

data:為數(shù)據(jù)框

col:需要被拆分的列

into:要拆分為的(多個)列, 通常用c()的形式進(jìn)行命名

sep : = " " 用于指定分隔符

remove:是否刪除被分割的列


用separate函數(shù)將上面的wideunite數(shù)據(jù)框還原:


widesep <- separate(wideunite, information,c("person","grade","score"), sep = "-")

widesep


? person grade score

1 ? ? ?A ? ? 5 ? ?89

2 ? ? ?B ? ? 6 ? ?98

3 ? ? ?C ? ? 4 ? ?90


可見separate()函數(shù)和unite()函數(shù)的功能是相反的.


五 缺失值填充


示例數(shù)據(jù)集,增加NA值


NAdata <- data.frame(person=c('A','B','C','D'),grade=c(5,NA,4,7),score=c(89,98,NA,89))

NAdata


? person grade score

1 ? ? ?A ? ? 5 ? ?89

2 ? ? ?B ? ?NA ? ?98

3 ? ? ?C ? ? 4 ? ?NA

4 ? ? ?D ? ? 7 ? ?89


計算x的均值和中位數(shù)

x_mean <- mean(NAdata$grade, na.rm = TRUE)

x_median <- median(NAdata$grade, na.rm = TRUE)

計算y的眾數(shù)

y_mode <- as.character(NAdata$score[which.max(table(NAdata$score))])


用特定值進(jìn)行NA填充:

NAdata2 <- replace_na(data = NAdata, replace = list(grade = x_mean, score = y_mode))

NAdata2


? person ? ?grade score

1 ? ? ?A 5.000000 ? ?89

2 ? ? ?B 5.333333 ? ?98

3 ? ? ?C 4.000000 ? ?89

4 ? ? ?D 7.000000 ? ?89


其他數(shù)據(jù)的預(yù)處理方法及缺失值的處理方法,待續(xù) 。。。


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容