Reputation: 1829
I have this data frame in long format in R which looks something like this..
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
and I want to convert this into the wide format..
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
I looked at reshape and melt, but I got messed up answers...
Thanks in advance
P.S: Not all rows have "Processing", "Approval" and "Finalize" as stage, some may have extra stages
Upvotes: 0
Views: 80
Reputation: 193677
This is a basic long to wide transformation.
Try one of the following:
## 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)
Upvotes: 5