R:使用在名称中共享模式的多列将数据重塑为更长的格式

R: reshaping data to longer format with multiple columns that share pattern in name

一段时间以来,我一直在努力处理数据集,以将其从全宽格式转换为全长格式。我设法把它变成了介于两者之间的形式。如下面的示例所示,数据采用基于 Cond 列的较长格式。问题是测量列名称中的“_Pre”和“_Post”必须是另一个因素,如 Cond,命名为 PrePost。这就是为什么我尝试的代码会产生行数过多的错误结果:

vars_PrePost <- grep("Pre|Post", colnames(df))

df2 <-
  df %>%
  gather(variable, value, vars_PrePost, -c(ID)) %>%                                      
  tidyr::separate(variable,  c("variable", "PrePost"), "_(?=[^_]+$)") %>%                
  spread(variable, value) 

这是玩具数据集:

df <- data.frame(stringsAsFactors=FALSE,
               ID = c("10", "10", "11", "11", "12", "12"),
           Age = c("23", "23", "31", "31", "24", "24"),
          Gender = c("m", "m", "m", "m", "f", "f"),
         Cond = c("Cond2", "Cond1", "Cond2", "Cond1", "Cond2", "Cond1"),
         Measure1_Post = c(NA, "7", NA, "3", NA, "2"),
          Measure1_Pre = c(NA, "3", NA, "2", NA, "2"),
         Measure2_Post = c("1.3968694273826", "0.799543118218161",
                      "1.44098109351048", "0.836960160696351",
                      "1.99568500539374", "1.75138016371597"),
          Measure2_Pre = c("1.19248628113128", "0.726244170934944",
                      "1.01175268267757", "1.26415857605178",
                      "2.35250186706497", "1.27070245573958"),
    Measure3_Post = c("73", "84", "50", "40", "97", "89"),
     Measure3_Pre = c("70", "63", "50", "46", "88", "71")
)

所需的输出应如下所示:

desired_df <- data.frame(stringsAsFactors=FALSE,
        Cond = c("Cond2", "Cond2", "Cond1", "Cond1", "Cond2", "Cond2", "Cond1",
                 "Cond1", "Cond2", "Cond2", "Cond1", "Cond1"),
     PrePost = c("Post", "Pre", "Post", "Pre", "Post", "Pre", "Post", "Pre",
                 "Post", "Pre", "Post", "Pre"),
    Measure1 = c(NA, NA, 7, 3, NA, NA, 3, 2, NA, NA, 2, 2),
    Measure2 = c(1.3968694273826, 1.19248628113128, 0.799543118218161,
                 0.726244170934944, 1.44098109351048, 1.01175268267757,
                 0.836960160696351, 1.26415857605178, 1.99568500539374,
                 2.35250186706497, 1.75138016371597, 1.27070245573958),
    Measure3 = c(73, 70, 84, 63, 50, 50, 40, 46, 97, 88, 89, 71)
)

我想要一个 tidy / dplyr 解决方案,但任何解决方案都会受到赞赏。谢谢你。

在 tidyr v1.0.0 中使用特殊动词 .valuenames_pattern 我们可以

library(tidyr) #v1.0.0
#select columns with _
pivot_longer(df, cols = matches('_'), 
                 names_to = c(".value","PrePost"), 
                 names_pattern = "(.*)_(.*)")

# A tibble: 12 x 8
   ID    Age   Gender Cond  PrePost Measure1 Measure2          Measure3
   <chr> <chr> <chr>  <chr> <chr>   <chr>    <chr>             <chr>   
 1 10    23    m      Cond2 Post    NA       1.3968694273826   73      
 2 10    23    m      Cond2 Pre     NA       1.19248628113128  70      
 3 10    23    m      Cond1 Post    7        0.799543118218161 84      
 4 10    23    m      Cond1 Pre     3        0.726244170934944 63  
 ...