[R] Reshape question

David Perlman dperlman at wisc.edu
Tue Mar 6 21:00:43 CET 2012


I have a data frame in wide format.  There are six variables that represent two factors in long format 3x2, Valence and Temperature:

> head(dpts)
              File Subj Time Group PainNeg.hot PainNeg.warm SociNeg.hot SociNeg.warm Positiv.hot Positiv.warm Errors
1 WB101_1_1_dp.txt  101    1   MNP   30.700000     13.75000   16.319048    35.166667    30.18333    14.383333      1
2 WB101_2_1_dp.txt  101    2   MNP    5.266667    -79.60000  -24.738095    -5.500000    23.95000   -14.700000      0
3 WB102_1_1_dp.txt  102    1   MNP   50.750000    -13.43214    5.185714    19.083333     4.20000    -8.033333      1
4 WB102_2_1_dp.txt  102    2   MNP  -41.383333      9.32500   -9.845238    22.950000   -14.35000    40.933333      0
5 WB103_1_1_dp.txt  103    1   MNP   25.266667    -48.27500   48.726190     8.141667    10.98333   -31.966667      2
6 WB103_2_1_dp.txt  103    2   MNP   26.750000    -13.28929    3.447619    -8.641667   -10.90000   -27.416667      1

The following command does part of what I want:

dptsr<-reshape(dpts,
varying=c('PainNeg.hot','PainNeg.warm','SociNeg.hot','SociNeg.warm','Positiv.hot','Positiv.warm'),
v.names=c('Bias'),direction='long',timevar=c('Valence','Temperature'),
times=c('PainNeg.hot','PainNeg.warm','SociNeg.hot','SociNeg.warm','Positiv.hot','Positiv.warm'),
idvar=c('Subj','Time'))

But it doesn't break out the two factors:

> head(dptsr)
                              File Subj Time Group Errors     Valence Temperature       Bias
101.1.PainNeg.hot WB101_1_1_dp.txt  101    1   MNP      1 PainNeg.hot PainNeg.hot  30.700000
101.2.PainNeg.hot WB101_2_1_dp.txt  101    2   MNP      0 PainNeg.hot PainNeg.hot   5.266667
102.1.PainNeg.hot WB102_1_1_dp.txt  102    1   MNP      1 PainNeg.hot PainNeg.hot  50.750000
102.2.PainNeg.hot WB102_2_1_dp.txt  102    2   MNP      0 PainNeg.hot PainNeg.hot -41.383333
103.1.PainNeg.hot WB103_1_1_dp.txt  103    1   MNP      2 PainNeg.hot PainNeg.hot  25.266667
103.2.PainNeg.hot WB103_2_1_dp.txt  103    2   MNP      1 PainNeg.hot PainNeg.hot  26.750000

So I did successfully create two factor variables, but they both contain the same values.  Instead I would want "Valence" to be (for example) "PainNeg" and "Temperature" to be "hot".

Can anyone help me figure out how to get reshape to do this?  I have never been able to make much sense out of the reshape documentation...

Thanks!



-dave----------------------------------------------------------------------
A neuroscientist is at the video arcade, when someone makes him a $1000 bet
on Pac-Man. He smiles, gets out his screwdriver and takes apart the Pac-Man
game. Everyone says "What are you doing?" The neuroscientist says "Well,
since we all know that Pac-Man is based on electric signals traveling
through these circuits, obviously I can understand it better than the other
guy by going straight to the source!"



More information about the R-help mailing list