I would like to convert a data frame from a long format to a wide format, but with unequal group sizes.
Possible use will be in "qcc", which requires a data frame or matrix with each row consisting of one group, using NA in groups with fewer samples.
The following code will create an example data set and also show manual conversion to the desired format.
So, I would like to convert this:
time measure 1 2001 Q1 0.14680685 2 2001 Q1 0.53593193 3 2001 Q1 0.56097974 4 2001 Q2 -1.48102689 5 2001 Q2 0.18150972 6 2001 Q3 1.72018147 7 2001 Q3 -0.08480855 8 2001 Q3 -2.23208877 9 2001 Q3 -1.15269107 10 2001 Q3 0.57975023
... to that...
[,1] [,2] [,3] [,4] [,5] 2001 Q1 0.1468068 0.53593193 0.5609797 NA NA 2001 Q2 -1.4810269 0.18150972 NA NA NA 2001 Q3 1.7201815 -0.08480855 -2.2320888 -1.152691 0.5797502
There is probably a simple way (maybe some use of reshape or reshape2 changes that I am not familiar with?), But a bunch of searches have not helped me yet.
Thanks for any help!
===========
In one of the solutions below, the following will generate the final qcc xbar graph, including group labels:
library(splitstackshape) out_df <- dcast( getanID( x_df, 'time' ), time~.id, value.var='measure' ) qcc( out_df[,-1], type = 'xbar', labels = out_df[,1] )