Search code examples
rreshapereshape2meltdcast

dcasting long table along multiple columns


I am having a difficult time understanding dcast, and cannot get the right commands to get what I want. I will give a minimal repro.

#generate the data
ID <- c('a','a','a','b','b','b')
Parameter <- c('p1','p2','p3','p1','p2','p3')
Value <- c('yes','no','3','yes','yes','2')
Comment <- c(NA,'Deduced','To verify',NA,'Deduced','Verified')
Source <- c('Exp.1','Exp.1','Exp.1+2','DB2','DB2','DB2')
Person <- c('X','X','X','Y','Y','Z')
long.data <- data.frame(ID,Parameter,Value,Comment,Source,Person)

  ID Parameter Value   Comment  Source Person
1  a        p1   yes      <NA>   Exp.1      X
2  a        p2    no   Deduced   Exp.1      X
3  a        p3     3 To verify Exp.1+2      X
6  b        p1   yes      <NA>     DB2      Y
7  b        p2   yes   Deduced     DB2      Y
8  b        p3     2  Verified     DB2      Y

I want to turn this into the following wide.data format:

  ID Person  p1 p1-Comment p1-Source   p2 p2-Comment p2-Source p3 p3-Comment p3-Source
1  a      X yes       <NA>     Exp.1   no    Deduced     Exp.1  3  To verify   Exp.1+2
2  b      Y yes       <NA>       DB2  yes    Deduced       DB2  2   Verified       DB2

I can assume that every ID has the same Person. I believed that I could dcast this, but I have not figured out a reasonable way to do this that doesn't output garbage columns. There is probably a relatively straightforward way to do this that I am just missing.


Solution

  • We could use pivot_wider

    library(tidyr)
    library(dplyr)
    pivot_wider(long.data, names_from = Parameter,
       values_from = c(Value, Comment, Source ), 
         names_glue = "{Parameter}-{.value}", names_vary = "slowest")) %>% 
       filter(!is.na(`p1-Value`))
    
    

    -output

    # A tibble: 2 × 11
      ID    Person `p1-Value` `p1-Comment` `p1-Source` `p2-Value` `p2-Comment` `p2-Source` `p3-Value` `p3-Comment` `p3-Source`
      <chr> <chr>  <chr>      <chr>        <chr>       <chr>      <chr>        <chr>       <chr>      <chr>        <chr>      
    1 a     X      yes        <NA>         Exp.1       no         Deduced      Exp.1       3          To verify    Exp.1+2    
    2 b     Y      yes        <NA>         DB2         yes        Deduced      DB2         <NA>       <NA>         <NA>