2015-09-14 94 views
0

的數據幀我在R中長格式這個數據幀看起來是這樣的..重塑中的R

Branch.Name Customer.ID Loan.Type Date  Stage  Time 
      A C001 Home  20/11/05 Processing 10 
      A C001 Home  20/11/05 Approval 30 
      A C001 Home  20/11/05 Finalize 5 
      A C002 Business 23/11/05 Processing 30 

,我想這個轉換爲寬幅..

Branch.Name Customer.ID Loan.Type Date  Processing Approval Finalize 
A   C001  Home  20/11/05 10  30  5 
B   C002  Business 20/11/05 30  10  0 
B   C003  Business 20/11/05 12  15  0 

我看着改造並融化,但還是搞砸了答案......

在此先感謝

PS:不是所有的行有「公關,「批准」和「敲定」爲舞臺,有些可能會有額外的階段

+0

我想你'Branch.Name'輸入數據應該有'B」。也許'庫(reshape2); dcast(df1,...〜Stage,value.var ='Time',fill = 0)' – akrun

回答

5

這是一個基本的長到寬的轉變。

請嘗試以下之一:

## Base R 
reshape(mydf, direction = "wide", 
     idvar = c("Branch.Name", "Customer.ID", "Loan.Type", "Date"), 
     timevar = "Stage") 

## reshape2 
library(reshape2) 
dcast(mydf, Branch.Name + Customer.ID + Loan.Type + Date ~ Stage, value.var = "Time") 
## dcast(mydf, ... ~ Stage, value.var = "Time") 

## tidyr 
library(tidyr) 
spread(mydf, Stage, Time) 
+0

感謝他們三人工作.. –