Reshape long to wide with multiple groupings

前端 未结 2 1137
误落风尘
误落风尘 2021-01-24 10:51

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         


        
2条回答
  •  梦毁少年i
    2021-01-24 11:20

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

提交回复
热议问题