在按 key_cols 区分的行之间添加一个空行

问题描述 投票:0回答:1

为了通过 key_cols 变量区分列表中的行,最好在其之间有一个空行,有一个例子:

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)

然后我们有:

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     

如果每个人之间都排成一排,那就太好了,就像:

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     

感谢您对此进行调查!

r nest
1个回答
0
投票

一种巧妙的方法是在列出之前操作数据,方法是将所有列转换为字符,按

USUBJID
拆分数据,向每个列添加一个空白行,然后重新组合在一起。

library(dplyr)

adae <- ex_adae %>% 
  mutate(across(everything(), as.character))

lsting <- as_listing(
  df = do.call(rbind, lapply(split(adae, ~USUBJID), \(x) {
    y <- setNames(data.frame(matrix(rep("", ncol(adae)), nr=1)), names(adae))
    y$USUBJID <- unique(x$USUBJID)
    bind_rows(x, y)
  })),
  disp_cols = c("ARM", "AGE", "SEX"),
  key_cols = c("USUBJID", "AETOXGR")
)

head(lsting, 15)

      USUBJID          AETOXGR        ARM         AGE   SEX             RACE                 AEDECOD       AESEV  
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————
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

请注意,首先显示空白行。

© www.soinside.com 2019 - 2024. All rights reserved.