Home > Blockchain >  Modifying weighted gt table based on unweighted data
Modifying weighted gt table based on unweighted data

Time:08-04

I am creating multiple weighted tables with multiple variables, and I'm curious about whether weighted values can be changed based on unweighted values. Specifically, when n<2, replace cell by NA.

Example data:

df <- data.frame(a= c("m","f","f","m", "m", "f", "f"),b= c("g1","g2","g1","g2", "g2", "g2", "g2"),
                 weight = c(1.1, 0.8, 2.2, 4, 3, 0.3, 1.9))

> df
  a  b weight
1 m g1    1.1
2 f g2    0.8
3 f g1    2.2
4 m g2    4.0
5 m g2    3.0
6 f g2    0.3
7 f g2    1.9

Table:

library(gtsummary)
library(srvyr)

tbl_summary_object <- df %>%
  as_survey_design(1, weight = weight) %>%
  gtsummary::tbl_svysummary(
    missing="no",
    by=a,
    include=-weight,
    label=list(
      b ~ "group"
    ),
    percent="row"
  )

enter image description here

I am looking to modify cells based on unweighted cells:

> table(df$a, df$b)
   
    g1 g2
  f  1  3
  m  1  2

I know that the "by" unweighted variable can be accessed tbl_summary_object$df_by. However, I am looking to change each cell based on n<2 in unweighted data. Any pointers would be greatly appreciated.

CodePudding user response:

We could do it this way:

If I understand you correctly, then we could access our object with tbl_summary_object[1]$table_body and manipulate it with an case_when, we also use parse_number which extracts always the first number and we then can do our comparison n < 2:

library(gtsummary)
library(dplyr)
library(readr) # parse_number

tbl_summary_object[1]$table_body  <- tbl_summary_object[1]$table_body %>% 
  mutate(across(c(stat_1, stat_2), ~ case_when(parse_number(.) < 2 ~ "NA",
                                               TRUE ~ .)))

tbl_summary_object

enter image description here

CodePudding user response:

We may use modify_table_body

library(dplyr)
library(gtsummary)
tbl_summary_object %>%
   modify_table_body(
  ~ .x %>%
    mutate(across(starts_with('stat_'), 
        ~ case_when(readr::parse_number(.x) >1 ~ .x))))

-output enter image description here


Based on the comments, perhaps this helps

library(tidyr)
tbl1 <- df %>% 
  count(a, b) %>% 
  mutate(a = recode(a, f = 'stat_1', m = 'stat_2'))  %>% 
  pivot_wider(names_from = a, values_from = n)
tbl_summary_object %>%
  modify_table_body(
  ~ .x %>%
    mutate(across(starts_with('stat_'), 
    ~ case_when(readr::parse_number(.x) > c(NA, tbl1[[cur_column()]]) ~ .x))))
    
  • Related