Search code examples
rexpss

Skip "zero" level of dichotomous variables in expss tables


I want to create a summary table for some dichotomous variables using the expss package. Since the variables are dichotomous, one of the two levels would the sufficient to "show the picture".

I tried to use the function tab_net_cell, but was not able to get the right results. Here is some example code with BrCa (Breast cancer) with 1 or 0. I only want to show the number of patients with but not without breast cancer.

df <- data.frame(BrCa = c(1,1,1,0,0,0,NA,NA,0,0))
df$group <- c(1,2,1,2,1,2,1,2,1,2)

df %>% 
  expss::tab_cols(group) %>%
  expss::tab_cells(BrCa)  %>%
  expss::tab_stat_cpct(total_row_position = "none",label = "%") %>%
  expss::tab_stat_cases(total_row_position = "none",label = "N") %>%
  expss::tab_pivot(stat_position = "inside_rows") 


df %>% 
  expss::tab_cols(group) %>%
  expss::tab_cells(BrCa)  %>%
  expss::tab_net_cells("BrCa" = eq(1))  %>%
  expss::tab_stat_cpct(total_row_position = "none",label = "%") %>%
  expss::tab_stat_cases(total_row_position = "none",label = "N") %>%
  expss::tab_pivot(stat_position = "inside_rows") 


Solution

  • The simplest way is to filter resulted table:

    df <- data.frame(BrCa = c(1,1,1,0,0,0,NA,NA,0,0))
    df$group <- c(1,2,1,2,1,2,1,2,1,2)
    
    df %>% 
        expss::tab_cols(group) %>%
        expss::tab_cells(BrCa)  %>%
        expss::tab_stat_cpct(total_row_position = "none",label = "%") %>%
        expss::tab_stat_cases(total_row_position = "none",label = "N") %>%
        expss::tab_pivot(stat_position = "inside_rows") %>% 
        expss::where(grepl(1, row_labels))
    

    Another way is to use mean and sum instead of cpct and cases:

    df %>% 
        expss::tab_cols(group) %>%
        expss::tab_cells(BrCa*100)  %>%
        expss::tab_stat_mean(label = "%") %>%
        expss::tab_stat_sum(label = "N") %>%
        expss::tab_pivot(stat_position = "inside_rows")