Reshape long to wide with multiple groupings

别等时光非礼了梦想. 提交于 2019-12-02 07:04:13

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!