Reshape long to wide with multiple groupings

2019-03-02 03:47发布

问题:

My data looks like this:

  Smoker PtNo Day Hour FEV1 timename
1       0    1   1    0 3.26     d1h0
2       0    1   1    2 3.05     d1h2
3       0    1   1    4 3.02     d1h4
4       0    1   1    6 3.27     d1h6
5       0    1   2    0 3.28     d2h0
6       0    1   2    2 3.07     d2h2
7       0    1   2    4 3.35     d2h4
8       0    1   2    6 3.07     d2h6
9       0    1   3    0 3.28     d3h0
10      0    1   3    2 3.44     d3h2

I want to reshape it into wide format like this:

PtNo  Smoker  FEV1.d1h0  FEV1.d1h2  FEV1d1.h3   etc.

Where PtNo and Smoker and independent variables not varying by time, and FEV1 is the measured time-varying variable. I get various error messages using reshape and the melt/dcast functions in the reshape2 package. Any suggestions? (Please tailor response to novice level.)

回答1:

Are you looking for this?

reshape(dat,direction='wide',
        idvar=c('Smoker','PtNo'),
        v.names='FEV1',
        timevar='timename',
        drop=c('Day','Hour'))

 Smoker PtNo FEV1.d1h0 FEV1.d1h2 FEV1.d1h4 FEV1.d1h6 FEV1.d2h0 FEV1.d2h2 FEV1.d2h4 FEV1.d2h6 FEV1.d3h0 FEV1.d3h2
1      0    1      3.26      3.05      3.02      3.27      3.28      3.07      3.35      3.07      3.28      3.44


回答2:

> dcast(df, PtNo + Smoker ~ timename, value.var="FEV1")
 PtNo Smoker   d1h0 d1h2 d1h4 d1h6 d2h0 d2h2 d2h4 d2h6 d3h0 d3h2
1   1      0   3.26 3.05 3.02 3.27 3.28 3.07 3.35 3.07 3.28 3.44

If you want the col names to be exactly as you have them, then you can just paste "FEV1" to timename before you dcast. dcast is from package reshape2.