本文是對(duì)[Sean C. Anderson ][1] 所介紹的reshape2進(jìn)行的整理迈套。
1. 序言
寬數(shù)據(jù)
# ozone wind temp
# 1 23.62 11.623 65.55
# 2 29.44 10.267 79.10
# 3 59.12 8.942 83.90
# 4 59.96 8.794 83.97
長(zhǎng)數(shù)據(jù)
# variable value
# 1 ozone 23.615
# 2 ozone 29.444
# 3 ozone 59.115
# 4 ozone 59.962
# 5 wind 11.623
# 6 wind 10.267
# 7 wind 8.942
# 8 wind 8.794
# 9 temp 65.548
# 10 temp 79.100
# 11 temp 83.903
# 12 temp 83.968
長(zhǎng)數(shù)據(jù)有一列數(shù)據(jù)是變量的類(lèi)型,有一列是變量的值资溃。長(zhǎng)數(shù)據(jù)不一定只有兩列大莫。ggplot2需要長(zhǎng)類(lèi)型的數(shù)據(jù),plyr也需要長(zhǎng)類(lèi)型的數(shù)據(jù)拓瞪,大多數(shù)的模型(比如lm()
, glm()
以及gam()
)也需要長(zhǎng)數(shù)據(jù)缴罗。
2. reshape2 包
reshape2 用得比較多的是melt
和cast
兩個(gè)函數(shù)。
-
melt
函數(shù)對(duì)寬數(shù)據(jù)進(jìn)行處理祭埂,得到長(zhǎng)數(shù)據(jù)面氓; -
cast
函數(shù)對(duì)長(zhǎng)數(shù)據(jù)進(jìn)行處理,得到寬數(shù)據(jù)蛆橡;
2.1 melt函數(shù)
此處用R內(nèi)置的airquality數(shù)據(jù)集舌界,首先將列名改成小寫(xiě),然后查看相應(yīng)的數(shù)據(jù)
names(airquality) <- tolower(names(airquality))
head(airquality)
ozone solar.r wind temp month day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
先看看直接用metl
函數(shù)處理上述的數(shù)據(jù)泰演,會(huì)有什么結(jié)果呻拌。
aql <- melt(airquality) # [a]ir [q]uality [l]ong format
head(aql)
variable value
1 ozone 41
2 ozone 36
3 ozone 12
4 ozone 18
5 ozone NA
6 ozone 28
然后再看看末尾的幾個(gè)數(shù)據(jù)
tail(aql)
variable value
913 day 25
914 day 26
915 day 27
916 day 28
917 day 29
918 day 30
默認(rèn)情況下,melt
認(rèn)為所有數(shù)值列的變量均有值睦焕。很多情況下柏锄,這都是我們想要的情況酿箭。在這里,我們想知道每個(gè)月(month)以及每天(day)的ozone, solar.r, wind以及temp的值趾娃。因此缭嫡,我們需要告訴melt
,month和day是"ID variables"抬闷。ID variables就是那些能夠區(qū)分不同行數(shù)據(jù)的變量妇蛀,個(gè)人感覺(jué)類(lèi)似于數(shù)據(jù)庫(kù)中的主鍵。
aql <- melt(airquality, id.vars = c("month", "day"))
head(aql)
month day variable value
1 5 1 ozone 41
2 5 2 ozone 36
3 5 3 ozone 12
4 5 4 ozone 18
5 5 5 ozone NA
6 5 6 ozone 28
如果我們想修改長(zhǎng)數(shù)據(jù)中的列名笤成,該如何操作呢?
aql <- melt(airquality, id.vars = c("month", "day"),
variable.name = "climate_variable",
value.name = "climate_value")
head(aql)
month day climate_variable climate_value
1 5 1 ozone 41
2 5 2 ozone 36
3 5 3 ozone 12
4 5 4 ozone 18
5 5 5 ozone NA
6 5 6 ozone 28
2.2 cast函數(shù)
從寬格式數(shù)據(jù)變換到長(zhǎng)格式的數(shù)據(jù)比較直觀评架,然后反過(guò)來(lái)則需要一些二外的功夫。
在reshape2中有好幾個(gè)cast
版本的函數(shù)炕泳。若你經(jīng)常使用data.frame纵诞,就需要使用dcast
函數(shù)。acast
函數(shù)返回向量培遵、矩陣或者數(shù)組浙芙。
dcast
借助于公式來(lái)描述數(shù)據(jù)的形狀,左邊參數(shù)表示"ID variables"籽腕,而右邊的參數(shù)表示measured variables嗡呼。可能需要幾次嘗試皇耗,才能找到合適的公式南窗。
這里,我們需要告知dcast
郎楼,month和day是ID variables万伤,variable則表示measured variables。
aql <- melt(airquality, id.vars = c("month", "day"))
aqw <- dcast(aql, month + day ~ variable)
head(aqw)
month day ozone solar.r wind temp
1 5 1 41 190 7.4 67
2 5 2 36 118 8.0 72
3 5 3 12 149 12.6 74
4 5 4 18 313 11.5 62
5 5 5 NA NA 14.3 56
6 5 6 28 NA 14.9 66
head(airquality) # original data
ozone solar.r wind temp month day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
除了需要調(diào)整下列變量的順序呜袁,我們已經(jīng)恢復(fù)出原始數(shù)據(jù)敌买。下圖將有助解釋所發(fā)生的情況。
![here is the dcast illustration][2]
藍(lán)色陰影塊是能夠表示每一行數(shù)據(jù)的ID variables傅寡;紅色陰影塊包含了將待生成數(shù)據(jù)的列名放妈;而灰色的數(shù)據(jù)表示用于填充相關(guān)區(qū)域的數(shù)據(jù)北救。
令人產(chǎn)生疑惑的情況往往是荐操,一個(gè)數(shù)據(jù)單元有一個(gè)以上的數(shù)據(jù)。比如珍策,我們的ID variables不包含day托启,
dcast(aql, month ~ variable)
month ozone solar.r wind temp
1 5 31 31 31 31
2 6 30 30 30 30
3 7 31 31 31 31
4 8 31 31 31 31
5 9 30 30 30 30
同時(shí)還有以下的警告信息
Aggregation function missing: defaulting to length
再次查看dcast
的輸出數(shù)據(jù),可以看到每個(gè)單元是month與climate組合的個(gè)數(shù)攘宙。所得到數(shù)據(jù)是month對(duì)應(yīng)的day的記錄數(shù)屯耸。當(dāng)每個(gè)單元有多個(gè)數(shù)據(jù)是拐迁,需要告訴dcast
如何聚合(aggregate
)這些數(shù)據(jù),比如取均值(mean
)疗绣,計(jì)算中位數(shù)(median
)线召,或者簡(jiǎn)單的求和(sum
)。比如多矮,在這里缓淹,我們簡(jiǎn)單的計(jì)算下均值,同時(shí)通過(guò)na.rm = TRUE
刪除NA值塔逃。
dcast(aql, month ~ variable, fun.aggregate = mean, na.rm = TRUE)
month ozone solar.r wind temp
1 5 23.61538 181.2963 11.622581 65.54839
2 6 29.44444 190.1667 10.266667 79.10000
3 7 59.11538 216.4839 8.941935 83.90323
4 8 59.96154 171.8571 8.793548 83.96774
5 9 31.44828 167.4333 10.180000 76.90000```
### 3. 參考文獻(xiàn)
本文源于[Sean C. Anderson][1]的文檔讯壶,具體的可以查看
<http://seananderson.ca/2013/10/19/reshape.html>
[1]: http://seananderson.ca/2013/10/19/reshape.html
[2]: http://upload-images.jianshu.io/upload_images/58036-d3cdd2487970a0a5.png "what is this "