Search code examples
rgtsummary

How to omit row but keep the frequencies with gtsummary in R?


I'm using the gtsummary package to elaborate a simple table. The code I'm using is the following

dados %>% select(ANO_OCORRENCIA, FAIXA_ETARIA, CS_RACA, ORIENT_SEX, IDENT_GEN, CAT_GEST) %>%
    tbl_summary(by = ANO_OCORRENCIA,
              digits = all_categorical() ~ c(0,1),
              label = list(FAIXA_ETARIA ~ "Faixa Etária",
                           CS_RACA ~ "Raça/cor da pele**",
                           ORIENT_SEX ~ "Orientação Sexual",
                           IDENT_GEN ~ "Identidade de Gênero",
                           CAT_GEST ~ "Gestantes")) %>%
    modify_spanning_header(all_stat_cols() ~ "**Ano de Ocorrência**") %>%
    add_overall() %>%
    modify_header(update = list( label ~ '**Características**',
                               stat_0  ~ '**Total**<br>N = 4,251' )) %>%
  modify_table_body(~.x %>% dplyr::relocate(stat_0, .after = stat_6),
                    filter, !(variable == "Gestantes" & label == "Não Gestante"))

The result:

enter image description here

I want to omit the row highlighted in red, so it doesn't change the frequencies. I've tried filter, !(variable == "Gestantes" & label == "Não Gestante") but it doesn't work. When I change variable == "Gestantes" to variable == "CS_GEST", an error message appears:

Error: An error occured in `add_overall()`, and overall statistics cannot be merged.
Has the variable label changed since the original call of `tbl_summary()`?

Is there any way I can do that?


Solution

  • here an example with mtcars data from the datasets package

    mtcars%>%
      select(mpg,cyl)%>%
      tbl_summary()%>%
       remove_row_type(variables = cyl,
                       type="level",
                       level_value = "8")
    

    gt remove

    For ref the original table without removing the row

    mtcars%>%
      select(mpg,cyl)%>%
      tbl_summary()
    

    ref

    You could find more detail here remove_row_type