Home > Back-end >  can tbl_summary() leave empty value instead of NA/ missing values?
can tbl_summary() leave empty value instead of NA/ missing values?

Time:01-15

I am using tbl_summary() to summarize clinical characteristics of a patient cohort.

I have a patient group and a control group. My problem is that I have more variables for the patient group (blood counts etc.) that are not available for the control group.

Example data:

library(gtsummary)

group <- rep(c("Kontrol","Patient"),times=c(5,10))
age <- floor(runif(15, min=20, max=101))
sex <- rep(c("male","female","male","female"), times=c(3,2,7,3))
Hemoglobin <-  as.numeric(c(rep("",times=5),7.21,7.52,10.41,10.03,8.95,6.24,8.54,14.22,8.76,7.57))

df <- data.frame(group,age,sex,Hemoglobin)

df %>% tbl_summary(by=group)

enter image description here

CodePudding user response:

Just adding another solution using modify_table_body(). This function allows for some more advanced customization of gtsummary tables.

library(gtsummary)

group <- rep(c("Kontrol","Patient"),times=c(5,10))
age <- floor(runif(15, min=20, max=101))
sex <- rep(c("male","female","male","female"), times=c(3,2,7,3))
Hemoglobin <-  as.numeric(c(rep("",times=5),7.21,7.52,10.41,10.03,8.95,6.24,8.54,14.22,8.76,7.57))

df <- data.frame(group,age,sex,Hemoglobin)

df %>% 
tbl_summary(by=group, missing="no") %>% 
 modify_table_body(~.x %>% 
                     dplyr::mutate(stat_1 = ifelse(stat_1 == "NA (NA, NA)",NA,stat_1)))
  •  Tags:  
  • Related