[R] reshape: melt and cast

Matt Pickard matthew.david.pickard at gmail.com
Tue Sep 1 06:14:10 CEST 2015

```Hi,

I have data that looks like this:

*> head(ratings)  QCode  PID  RaterName SI1 SI2 SI3 SI4 SI5 SI6 SI7 SI8 SI9
SI10 SI111 GUILT 1123 cwormhoudt   2   2   3   1   1   1   3   3   3
2    12  LOVE 1123 cwormhoudt   1   2   3   2   1   1   1   1   1    1
33 GUILT 1136 cwormhoudt   1   2   3   1   1   1   2   3   2    2    14
LOVE 1136 cwormhoudt   1   2   3   1   1   1   1   1   1    1    25 GUILT
1137 cwormhoudt   2   2   2   1   1   1   2   3   1    2    16  LOVE 1137
cwormhoudt   1   3   4   1   1   1   1   1   1    1    4*

*> tail(ratings)      QCode  PID RaterName SI1 SI2 SI3 SI4 SI5 SI6 SI7 SI8
SI9 SI10 SI112456    FUN 1555  zspeidel   1   3   3   1   1   1   1   1
1    1    12457    FUN 1556  zspeidel   1   2   2   1   1   1   1   1
1    1    12458    FUN 1558  zspeidel   1   2   3   1   1   1   1   1
1    1    12459 APPEAR 1558  zspeidel   1   3   3   1   1   1   2   1
1    2    12460 APPEAR 1559  zspeidel   1   3   3   1   1   1   2   1
1    2    12461    FUN 1559  zspeidel   1   2   2   1   1   1   1   1
1    1    1*
I am trying to use the melt and cast functions to re-arrange it to look
like this:

*   QCode  PID sItem cwormhoudt zspeidel1 APPEAR 1123   SI1
1        12 APPEAR 1123   SI2          4        13 APPEAR 1123
SI3          1        24 APPEAR 1123   SI4          3        15 APPEAR
1123   SI5          1        16 APPEAR 1123   SI6          1        3*
So, I melt the data like this:

*mratings = melt(ratings, variable_name="sItem")*
Then cast the data like this:

*> outData = cast(mratings, QCode + PID + sItem ~ RaterName)Aggregation
requires fun.aggregate: length used as default*

But the value columns appear to be displaying counts and not the original
values:

*> head(outData)   QCode  PID sItem cwormhoudt zspeidel1 APPEAR 1123
SI1          1        12 APPEAR 1123   SI2          1        13 APPEAR
1123   SI3          1        14 APPEAR 1123   SI4          1        15
APPEAR 1123   SI5          1        16 APPEAR 1123   SI6          1
1> which(outData\$zpeidel==3)integer(0)*
How to I prevent cast from aggregating the data according to counts?  Am I
doing something wrong?