提迪尔-散布多列 [英] tidyr - spread multiple columns

查看:90
本文介绍了提迪尔-散布多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备用于网络元分析的数据,而且很难在列中进行键入.

I'm preparing data for a network meta-analysis and I am having difficult in tyding the columns.

如果我有这个初始数据集:

If I have this initial dataset:

Study Trt       y    sd   n
1       1   -1.22  3.70  54
1       3   -1.53  4.28  95
2       1   -0.30  4.40  76 
2       2   -2.60  4.30  71
2       4    -1.2   4.3  81

我该如何完成另一个?

Study Treatment1    y1  sd1  n1 Treatment2    y2  sd2  n2 Treatment3   y3 sd3 n3
    1     1          1 -1.22 3.70  54          3 -1.53 4.28  95         NA   NA  NA NA
    2     3          1 -0.30 4.40  76          2 -2.60 4.30  71          4 -1.2 4.3 81

我真的很困在这一步,非常感谢您的帮助...

I'm really stuck in this step, and I'd really appreciate some help...

推荐答案

我们可以将gather转换为'long'格式,然后将unite多列转换为单列,然后将spread转换为wide

We can gather to 'long' format, then unite multiple columns to single and spread it to wide

library(tidyverse)
gather(df1, Var, Val,  Trt:n) %>%
      group_by(Study, Var) %>% 
      mutate(n = row_number()) %>%
      unite(VarT, Var, n, sep="") %>%
      spread(VarT, Val, fill=0)

这篇关于提迪尔-散布多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆