[R] Reshape question

David Winsemius dwinsemius at comcast.net
Tue Mar 6 23:20:56 CET 2012


On Mar 6, 2012, at 3:00 PM, David Perlman wrote:

> 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'),

Try instead:

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.Temp'),
times=c('PainNeg.hot','PainNeg.warm',
            'SociNeg.hot','SociNeg.warm',
            'Positiv.hot', 'Positiv.warm')  ,
idvar=c('Subj','Time'))

dptsr$Valence <- sub("\\..+$", "", dptsr$Valence.Temp)
dptsr$Temp <- sub("^.+\\.", "", dptsr$Valence.Temp)

I admit that I haven't figured out how to do it on one step within  
reshape. The should be a way, but I have tried a bunch of (failed)  
methods.

-- 
David.


> 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...

I am very sympathetic to that lament.


>
> Thanks!
>


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list