Changing row labels for frequency distribution tables

Describe your issue

Hello! I frequently encounter datasets where values are not presentation-ready like in the dataset below:

 demo_data <- data.frame(
     stringsAsFactors = FALSE,
                  sex = c("m","m","m",
                          "m","m","m","m","m","f",
                          "m"),
  case_classification = c("prob","susp","prob",
                          "susp","susp","susp","susp","susp","susp","susp")
)

When I create a table for presentation, I am able to manipulate column labels with cols_label() for {gt} or set_header_labels() for {flextable}. However, I can’t seem to find an option for either to change the row labels for example:

In sex,
m to β€œMale”
f to β€œFemale”

In case_classification
susp to β€œSuspect”
prob to β€œProbable”

What steps have you already taken to find an answer?

What I am doing now is to recode values, for example:

demo_data <- demo_data %>%  mutate(sex = recode(sex, "m" = "Male"))

Is it possible to β€œrecode” or change row labels as you are constructing the table for presentation already? It does not look like it is possible for {flextable}, while for {gt} you can only add row group labels with tab_row_group().

2 Likes

hey Ian - check out the labelled package - it works with gtsummary which can then be passed to gt or flextable.

Here is some example code for how to set labels - hope this helps

1 Like

Hi Ian,

In addition to using the labelled package, as Alex suggested, you could also use case_match and fmt to achieve this, as seen below:

library(tidyverse)
library(gt)

demo_data <- data.frame(
    stringsAsFactors = FALSE,
    sex = c("m", "m", "m",
                    "m", "m", "m", "m", "m", "f",
                    "m"),
    case_classification = c(
        "prob",
        "susp",
        "prob",
        "susp",
        "susp",
        "susp",
        "susp",
        "susp",
        "susp",
        "susp"
    )
)

gt(data = demo_data) |>
    cols_label(sex = "Sex",
                         case_classification = "Case Classification") |>
    fmt(columns = sex,
            fns = \(x) {
                case_match(
                    x,
                    "m" ~ "Male",
                    "f" ~ "Female",
                    .default = NA_character_
                )
            }) |>
    fmt(columns = case_classification,
            fns = \(x) {
                case_match(
                    x,
                    "prob" ~ "Probable",
                    "susp" ~ "Suspect",
                    .default = NA_character_
                )
            })
Sex Case Classification
Male Probable
Male Suspect
Male Probable
Male Suspect
Male Suspect
Male Suspect
Male Suspect
Male Suspect
Female Suspect
Male Suspect

Created on 2023-04-04 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.3 (2023-03-15)
#>  os       macOS Big Sur ... 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2023-04-04
#>  pandoc   2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.1   2023-03-23 [1] CRAN (R 4.2.0)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.2.0)
#>  digest        0.6.31  2022-12-11 [1] CRAN (R 4.2.0)
#>  dplyr       * 1.1.1   2023-03-22 [1] CRAN (R 4.2.0)
#>  evaluate      0.20    2023-01-17 [1] CRAN (R 4.2.0)
#>  fansi         1.0.4   2023-01-22 [1] CRAN (R 4.2.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.2.0)
#>  forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.2.2)
#>  fs            1.6.1   2023-02-06 [1] CRAN (R 4.2.0)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.0)
#>  ggplot2     * 3.4.1   2023-02-10 [1] CRAN (R 4.2.2)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
#>  gt          * 0.8.0   2022-11-16 [1] CRAN (R 4.2.0)
#>  gtable        0.3.3   2023-03-21 [1] CRAN (R 4.2.0)
#>  hms           1.1.3   2023-03-21 [1] CRAN (R 4.2.0)
#>  htmltools     0.5.5   2023-03-23 [1] CRAN (R 4.2.0)
#>  knitr         1.42    2023-01-25 [1] CRAN (R 4.2.0)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.1)
#>  lubridate   * 1.9.2   2023-02-10 [1] CRAN (R 4.2.2)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.2.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.2.3)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
#>  purrr       * 1.0.1   2023-01-10 [1] CRAN (R 4.2.2)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.2.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.2  2022-11-11 [1] CRAN (R 4.2.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
#>  readr       * 2.1.4   2023-02-10 [1] CRAN (R 4.2.2)
#>  reprex        2.0.2   2022-08-17 [1] RSPM (R 4.2.1)
#>  rlang         1.1.0   2023-03-14 [1] CRAN (R 4.2.2)
#>  rmarkdown     2.21    2023-03-26 [1] CRAN (R 4.2.0)
#>  rstudioapi    0.14    2022-08-22 [1] RSPM (R 4.2.1)
#>  sass          0.4.5   2023-01-24 [1] CRAN (R 4.2.0)
#>  scales        1.2.1   2022-08-20 [1] RSPM (R 4.2.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
#>  stringi       1.7.12  2023-01-11 [1] CRAN (R 4.2.2)
#>  stringr     * 1.5.0   2022-12-02 [1] CRAN (R 4.2.0)
#>  styler        1.9.1   2023-03-04 [1] CRAN (R 4.2.0)
#>  tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.2.3)
#>  tidyr       * 1.3.0   2023-01-24 [1] CRAN (R 4.2.0)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.2.0)
#>  tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.2.2)
#>  timechange    0.2.0   2023-01-11 [1] CRAN (R 4.2.2)
#>  tzdb          0.3.0   2022-03-28 [1] CRAN (R 4.2.0)
#>  utf8          1.2.3   2023-01-31 [1] CRAN (R 4.2.2)
#>  vctrs         0.6.1   2023-03-22 [1] CRAN (R 4.2.3)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.38    2023-03-24 [1] CRAN (R 4.2.0)
#>  yaml          2.3.7   2023-01-23 [1] CRAN (R 4.2.0)
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.2/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.2/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim

1 Like

Thank you Alex and Tim! I’ll look into both the labelled package and fmt() function of tthe gt package. Both seem to work great.

Regards,
Ian

2 Likes

Hi again, Tim! I am trying to understand this portion of the code using the fmt() function:

fns = \(x) {
                case_match(
                    x,
                    "m" ~ "Male",
                    "f" ~ "Female",
                    .default = NA_character_
                )
            }

I searched the {gt} documentation which showed that fns could be a single formatting function or a named list of functions. However, the given examples doesn’t look similar to how we applied it.

I understand how the nested case_match() function in our current example works, but how does the \(x) { } work around it?

Thank you.

1 Like

Hello Ian,

This is just shorthand R syntax for an anonymous function, you could replace it with:

fns = function(x) {
                case_match(
                    x,
                    "m" ~ "Male",
                    "f" ~ "Female",
                    .default = NA_character_
                )
            }

If you are more comfortable with that. Alternatively, you could try to write a standalone function if you plan on using it to format various variables in the table and then just call it within the fmt function with the necessary argument values for that given variable.

All the best,

Tim

1 Like

Thanks, Tim! The examples I saw used function(x). I thought the backslash served a different purpose.

Best,
Ian

1 Like