KatjaKaa
KatjaKaa

Reputation: 33

can tbl_summary() leave empty value instead of NA/ missing values?

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

so my question is can I make a table where these variables will have an empty value in the control column? something like: enter image description here

Thanks for your help!

Upvotes: 3

Views: 3092

Answers (2)

Mike
Mike

Reputation: 4370

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)))

Upvotes: 2

Claudiu Papasteri
Claudiu Papasteri

Reputation: 2609

It seems that you can't achieve the desired results only by using the arguments provided by the function (e.g. missing and missing_text arguments). You have to locally modify the list object.

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)

table <- 
  df %>% 
  tbl_summary(by=group, missing="no")  

table$table_body$stat_1[table$table_body$stat_1 == "NA (NA, NA)"] <- NA

table

Created on 2022-01-14 by the reprex package (v2.0.1)

enter image description here

Upvotes: 3

Related Questions