tidyr - 展开多列 [英] tidyr - spread multiple columns

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

问题描述

我正在为网络荟萃分析准备数据,但在输入列时遇到困难.

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多列到single和传播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)

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

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