Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to format numbers (comma) on a gt table

I have a gt table similar to the one in the example below. Some of the summary_rows and grand_summary_rows are comma formatted but others are not. Question: how can I format the entire gt table so that all the rows are comma formatted? I know about big.mark ="," but couldn’t find a way to add it to the code.

library(gt)
library(tidyverse)

sp500 %>%
  dplyr::filter(date >= "2015-01-05" & date <= "2015-01-16") %>%
  dplyr::arrange(date) %>%
  dplyr::mutate(week = paste0("W", strftime(date, format = "%V"))) %>%
  dplyr::select(-adj_close, -volume) %>%
  gt(
    rowname_col = "date",
    groupname_col = "week"
  ) %>%
  grand_summary_rows(
    columns = c(open, high, low, close),
    fns = list(
      min = ~min(.),
      max = ~max(.),
      avg = ~mean(.)),
    formatter = fmt_number,
    use_seps = FALSE
  )

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Reproducible example is always welcome. With this data, you may use this code:

sp500 %>%
    dplyr::filter(date >= "2015-01-05" & date <= "2015-01-16") %>%
    dplyr::arrange(date) %>%
    dplyr::mutate(week = paste0("W", strftime(date, format = "%V"))) %>%
    dplyr::select(-adj_close, -volume) %>%
    gt(
            rowname_col = "date",
            groupname_col = "week"
    ) %>% fmt_number(columns = 2:5, decimals = 2, sep_mark = ".", dec_mark = ",") %>% 
    grand_summary_rows(
            columns = c(open, high, low, close),
            fns = list(
                    min = ~min(.),
                    max = ~max(.),
                    avg = ~mean(.)),
            formatter = fmt_number,
            use_seps = FALSE
    )

That gives the result:

2.054,44    2.054,44    2.017,34    2.020,58

Hope this help.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading