rnest

Add a empty row between rows differentiated by key_cols


In order to distinguish rows in listing by key_cols variable by variable, it would be nice to have a empty row in between, there is an example:

library(rlistings)
adae <- ex_adae
lsting <- as_listing(
  df = adae,
  disp_cols = c("ARM", "AGE", "SEX", "RACE", "AEDECOD", "AESEV"),
  key_cols = c("USUBJID", "AETOXGR")
)
head(lsting, 15)

then we have:

Unique Subject Identifier   Analysis Toxicity Grade   Description of Planned Arm   Age   Sex             Race              Dictionary-Derived Term   Severity/Intensity
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  AB12345-BRA-1-id-134                 2                      A: Drug X            47     M              WHITE                  dcd A.1.1.1.2             MODERATE     
                                                              A: Drug X            47     M              WHITE                  dcd A.1.1.1.2             MODERATE     
                                       3                      A: Drug X            47     M              WHITE                  dcd B.2.1.2.1             MODERATE     
                                                              A: Drug X            47     M              WHITE                  dcd D.1.1.4.2             MODERATE     
  AB12345-BRA-1-id-141                 1                    C: Combination         35     F              WHITE                  dcd D.2.1.5.3               MILD       
                                                            C: Combination         35     F              WHITE                  dcd A.1.1.1.1               MILD       
                                                            C: Combination         35     F              WHITE                  dcd A.1.1.1.1               MILD       
                                       2                    C: Combination         35     F              WHITE                  dcd A.1.1.1.2             MODERATE     
                                       3                    C: Combination         35     F              WHITE                  dcd B.2.1.2.1             MODERATE     
                                       5                    C: Combination         35     F              WHITE                  dcd D.1.1.1.1              SEVERE      
  AB12345-BRA-1-id-236                 5                      B: Placebo           32     M    BLACK OR AFRICAN AMERICAN        dcd B.1.1.1.1              SEVERE      
                                                              B: Placebo           32     M    BLACK OR AFRICAN AMERICAN        dcd B.1.1.1.1              SEVERE      
                                                              B: Placebo           32     M    BLACK OR AFRICAN AMERICAN        dcd B.1.1.1.1              SEVERE      
  AB12345-BRA-1-id-265                 2                    C: Combination         25     M              WHITE                  dcd C.2.1.2.1             MODERATE     
                                       3                    C: Combination         25     M              WHITE                  dcd D.1.1.4.2             MODERATE     

It would truly be nice to have a row in between each person, be like:

Unique Subject Identifier   Analysis Toxicity Grade   Description of Planned Arm   Age   Sex             Race              Dictionary-Derived Term   Severity/Intensity
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  AB12345-BRA-1-id-134                 2                      A: Drug X            47     M              WHITE                  dcd A.1.1.1.2             MODERATE     
                                                              A: Drug X            47     M              WHITE                  dcd A.1.1.1.2             MODERATE     
                                       3                      A: Drug X            47     M              WHITE                  dcd B.2.1.2.1             MODERATE     
                                                              A: Drug X            47     M              WHITE                  dcd D.1.1.4.2             MODERATE     
  
  AB12345-BRA-1-id-141                 1                    C: Combination         35     F              WHITE                  dcd D.2.1.5.3               MILD       
                                                            C: Combination         35     F              WHITE                  dcd A.1.1.1.1               MILD       
                                                            C: Combination         35     F              WHITE                  dcd A.1.1.1.1               MILD       
                                       2                    C: Combination         35     F              WHITE                  dcd A.1.1.1.2             MODERATE     
                                       3                    C: Combination         35     F              WHITE                  dcd B.2.1.2.1             MODERATE     
                                       5                    C: Combination         35     F              WHITE                  dcd D.1.1.1.1              SEVERE      
  
  AB12345-BRA-1-id-236                 5                      B: Placebo           32     M    BLACK OR AFRICAN AMERICAN        dcd B.1.1.1.1              SEVERE      
                                                              B: Placebo           32     M    BLACK OR AFRICAN AMERICAN        dcd B.1.1.1.1              SEVERE      
                                                              B: Placebo           32     M    BLACK OR AFRICAN AMERICAN        dcd B.1.1.1.1              SEVERE      
  
  AB12345-BRA-1-id-265                 2                    C: Combination         25     M              WHITE                  dcd C.2.1.2.1             MODERATE     
                                       3                    C: Combination         25     M              WHITE                  dcd D.1.1.4.2             MODERATE     

Thank you for looking into this!


Solution

  • One hacky way would be to manipulate the data prior to listing by splitting the data by USUBJID, adding a blank row to each, and then combining them back together.

    library(dplyr)
    
    adae <- do.call(rbind, lapply(split(ex_adae, ~USUBJID), \(x) {
      y <- setNames(data.frame(matrix(rep(NA, ncol(ex_adae)), nr=1)), names(ex_adae))
      y$USUBJID <- unique(x$USUBJID)
      rbind(x, y)
    }))
    

    This removes the variable labels, which can be fixed using the sjlabelled package.

    library(sjlabelled)
    adae <- copy_labels(adae, ex_adae)
    
    lsting <- as_listing(
      df = adae,
      disp_cols = c("ARM", "AGE", "SEX", "RACE", "AEDECOD", "AESEV"),
      key_cols = c("USUBJID", "AETOXGR")
    )
    
    head(lsting, 15)
    

    Unique Subject Identifier   Analysis Toxicity Grade   Description of Planned Arm   Age   Sex             Race              Dictionary-Derived Term   Severity/Intensity
    ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
      AB12345-BRA-1-id-134                 2                      A: Drug X            47     M              WHITE                  dcd A.1.1.1.2             MODERATE     
                                                                  A: Drug X            47     M              WHITE                  dcd A.1.1.1.2             MODERATE     
                                           3                      A: Drug X            47     M              WHITE                  dcd B.2.1.2.1             MODERATE     
                                                                  A: Drug X            47     M              WHITE                  dcd D.1.1.4.2             MODERATE     
                                                                                                                                                                           
      AB12345-BRA-1-id-141                 1                    C: Combination         35     F              WHITE                  dcd D.2.1.5.3               MILD       
                                                                C: Combination         35     F              WHITE                  dcd A.1.1.1.1               MILD       
                                                                C: Combination         35     F              WHITE                  dcd A.1.1.1.1               MILD       
                                           2                    C: Combination         35     F              WHITE                  dcd A.1.1.1.2             MODERATE     
                                           3                    C: Combination         35     F              WHITE                  dcd B.2.1.2.1             MODERATE     
                                           5                    C: Combination         35     F              WHITE                  dcd D.1.1.1.1              SEVERE      
                                                                                                                                                                           
      AB12345-BRA-1-id-236                 5                      B: Placebo           32     M    BLACK OR AFRICAN AMERICAN        dcd B.1.1.1.1              SEVERE      
                                                                  B: Placebo           32     M    BLACK OR AFRICAN AMERICAN        dcd B.1.1.1.1              SEVERE      
                                                                  B: Placebo           32     M    BLACK OR AFRICAN AMERICAN        dcd B.1.1.1.1              SEVERE