tidyr - spread multiple columns

后端 未结 1 1529
南旧
南旧 2020-12-11 05:49

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            


        
1条回答
  •  [愿得一人]
    2020-12-11 06:34

    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)
    

    0 讨论(0)
提交回复
热议问题