R:reshape2包

一、cast函數數組

cast函數,把長型數據轉換成你想要的任何寬型數據:函數

dcast(data, formula, fun.aggregate = NULL, ..., margins = NULL, subset = NULL, fill = NULL, drop = TRUE, value.var = guess_value(data))
acast(data, formula, fun.aggregate = NULL, ..., margins = NULL, subset = NULL, fill = NULL, drop = TRUE, value.var = guess_value(data))
#參數:
#data  要進行轉換的數據框
#formula  用於轉換的公式
#fun.aggregate   聚合函數,表達式爲:行變量~列變量~三維變量~
#margins  用於添加邊界彙總數據
#subset   用於添加過濾條件,須要載入plyr包

acast,dcast的區別在於輸出結果。acast 輸出結果爲vector/matrix/array,dcast 輸出結果爲data.frame。code

> library(reshape2)
> x<-data.frame(id=1:6,name=c("wang","zhang","li","chen","zhao","song"),shuxue=c(89,85,68,79,96,53),yuwen=c(77,68,86,87,92,63))
> x
  id  name shuxue yuwen
1  1  wang     89    77
2  2 zhang     85    68
3  3    li     68    86
4  4  chen     79    87
5  5  zhao     96    92
6  6  song     53    63
> x1<-melt(x,id=c("id","name"))    #先使用melt函數對數據進行融化操做,把數據已經變成長型數據
> x1
   id  name variable value
1   1  wang   shuxue    89
2   2 zhang   shuxue    85
3   3    li   shuxue    68
4   4  chen   shuxue    79
5   5  zhao   shuxue    96
6   6  song   shuxue    53
7   1  wang    yuwen    77
8   2 zhang    yuwen    68
9   3    li    yuwen    86
10  4  chen    yuwen    87
11  5  zhao    yuwen    92
12  6  song    yuwen    63
> x2<-acast(x1,id~variable)
> x2
  shuxue yuwen
1     89    77
2     85    68
3     68    86
4     79    87
5     96    92
6     53    63
> x3<-dcast(x1,id~variable)
> x3
  id shuxue yuwen
1  1     89    77
2  2     85    68
3  3     68    86
4  4     79    87
5  5     96    92
6  6     53    63    
#從以上兩個執行結果來看,能夠看出acast和dcast的區別:這裏acast輸出結果省略了id這個列,而dcast則輸出id列。
> x4<-acast(x1,id~name~variable)
> x4
, , shuxue

  chen li song wang zhang zhao
1   NA NA   NA   89    NA   NA
2   NA NA   NA   NA    85   NA
3   NA 68   NA   NA    NA   NA
4   79 NA   NA   NA    NA   NA
5   NA NA   NA   NA    NA   96
6   NA NA   53   NA    NA   NA

, , yuwen

  chen li song wang zhang zhao
1   NA NA   NA   77    NA   NA
2   NA NA   NA   NA    68   NA
3   NA 86   NA   NA    NA   NA
4   87 NA   NA   NA    NA   NA
5   NA NA   NA   NA    NA   92
6   NA NA   63   NA    NA   NA
#三維的狀況下acast輸出的是一個數組,而dcast則報錯,由於dcast輸出結果爲數據框。
> x5<-dcast(x1,id~variable,mean,margins=T)
> x5
     id   shuxue    yuwen    (all)
1     1 89.00000 77.00000 83.00000
2     2 85.00000 68.00000 76.50000
3     3 68.00000 86.00000 77.00000
4     4 79.00000 87.00000 83.00000
5     5 96.00000 92.00000 94.00000
6     6 53.00000 63.00000 58.00000
7 (all) 78.33333 78.83333 78.58333
#能夠看到,邊緣多了兩列彙總數據是對行列求平均的結果
> x6<-dcast(x1,id~variable,mean,margins=c("id"))
> x6
     id   shuxue    yuwen
1     1 89.00000 77.00000
2     2 85.00000 68.00000
3     3 68.00000 86.00000
4     4 79.00000 87.00000
5     5 96.00000 92.00000
6     6 53.00000 63.00000
7 (all) 78.33333 78.83333
#只對列求平均值,固然也能夠只對行求平均值,把id改爲variable就能夠了
> x7<-dcast(x1,id~variable,mean,margins=c("variable"))    
> x7
  id shuxue yuwen (all)
1  1     89    77  83.0
2  2     85    68  76.5
3  3     68    86  77.0
4  4     79    87  83.0
5  5     96    92  94.0
6  6     53    63  58.0
> library(plyr)
> x8<-dcast(x1,id~variable,mean,subset=.(id==1|id==3))
> x8
  id shuxue yuwen
1  1     89    77
2  3     68    86
#subset的篩選功能強大能夠進行各類各樣的篩選操做,相似filter的做用

二、melt函數orm

> x
  id  name shuxue yuwen
1  1  wang     89    77
2  2 zhang     85    68
3  3    li     68    86
4  4  chen     79    87
5  5  zhao     96    92
6  6  song     53    63
> x9<-melt(x,id=1:2,variable.name="課目",value.name="值")
> x9
   id  name   課目 值
1   1  wang shuxue 89
2   2 zhang shuxue 85
3   3    li shuxue 68
4   4  chen shuxue 79
5   5  zhao shuxue 96
6   6  song shuxue 53
7   1  wang  yuwen 77
8   2 zhang  yuwen 68
9   3    li  yuwen 86
10  4  chen  yuwen 87
11  5  zhao  yuwen 92
12  6  song  yuwen 63
相關文章
相關標籤/搜索