1

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

R learner
  • 101
  • 7

1 Answers1

2

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") 
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • 1
    Thanks for the hint with the mean und sum. Now, this is my solution `df %>% expss::tab_cols(group) %>% expss::tab_cells(BrCa) %>% expss::tab_stat_fun("%" = function (x) mean(x,na.rm = T)*100, "N" = function (x) sum(x,na.rm = T)) %>% expss::tab_pivot(stat_position = "inside_rows")` – R learner Sep 02 '19 at 10:03
  • Could you please add the filtering solution? – R learner Sep 02 '19 at 10:07
  • @SilkeZachariae Sorry, I pasted the same code twice. See update of the first piece of code. – Gregory Demin Sep 02 '19 at 14:16