Reshaping several variables wide with cast Reshaping several variables wide with cast r r

Reshaping several variables wide with cast


I think the problem is that ff.df is not yet sufficiently molten. Try this:

library(reshape)# Melt it downff.melt <- melt(ff.df, id.var = c("surveyNum", ".id"))# Note the new "variable" column, which will be combined# with .id to make each column headerhead(ff.melt)  surveyNum .id variable value1         1   1      pio     22         2   1      pio     23         3   1      pio     14         4   1      pio     25         5   1      pio     16         6   1      pio     1# Cast it out - note that .id comes after variable in the formula;# I think the only effect of that is that you get "pio_1" instead of "1_pio"ff.cast <- cast(ff.melt, surveyNum ~ variable + .id)head(ff.cast)  surveyNum pio_1 pio_2 pio_3 caremgmt_1 caremgmt_2 caremgmt_3 prev_1 prev_2 prev_3 price_1 price_2 price_31         1     2     2     2          2          1          1      1      2      2       2       6       32         2     2     1     2          1          2          2      2      2      1       1       5       53         3     1     2     1          1          2          1      2      1      2       2       5       24         4     2     1     1          2          2          2      1      2      2       5       4       55         5     1     2     2          1          2          1      1      1      1       3       4       46         6     1     2     1          2          1          1      2      1      1       4       2       5

Does that do the trick for you?

Essentially, when casting, the variables indicated on the right-hand side of the casting formula dictate the columns that will appear in the cast result. By indicating only .id, I believe that you were asking cast to somehow cram all of those vectors of values into just three columns - 1, 2, and 3. Melting the data all the way down creates the variable column, which lets you specify that the combination of the .id and variable vectors should define the columns of the cast data frame.

(Sorry if I'm being repetitious/pedantic! I'm trying to work it out for myself, too)


You can do this with the reshape function in base R. The column ordering is a little weird, but can be fixed easily.

 reshape(ff.df, direction = 'wide', idvar = "surveyNum", timevar = '.id')


You could do this using dcast from the devel version of data.table i.e. v1.9.5

  library(data.table)  ff.cast <- dcast(setDT(ff.df), surveyNum~.id,         value.var=c('pio', 'caremgmt', 'prev', 'price'))  head(ff.cast,3)  #  surveyNum 1_pio 2_pio 3_pio 1_caremgmt 2_caremgmt 3_caremgmt 1_prev 2_prev  #1:         1     2     2     2          2          1          1      1      2  #2:         2     2     1     2          1          2          2      2      2  #3:         3     1     2     1          1          2          1      2      1  #   3_prev 1_price 2_price 3_price  #1:      2       2       6       3  #2:      1       1       5       5  #3:      2       2       5       2