在gtsummary中按降序排列行,在RMarkdown上不起作用?

7xzttuei  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(90)

我的数据集看起来像这样:

data <- data.frame(
  anoobito = sample(2019:2023),
  NOME_MUNICIPIO = sample(c("Rio de Janeiro", "São Paulo", "Araruama", "Queimados"), 230, replace = TRUE)
)

字符串
当我在RStudio中使用gtsummary按整个列降序排序时,它工作得很好。

data %>%
  select(anoobito, NOME_MUNICIPIO) %>%
  tbl_summary(by = anoobito,
              label = c(NOME_MUNICIPIO ~ "")) %>%
  add_overall(last = T) %>%
  modify_table_body(fun = ~ dplyr::arrange(., desc(readr::parse_number(stat_0)))) %>%
  modify_spanning_header(c("stat_1":"stat_5") ~ "**Ano do Óbito**") %>%
  modify_header(label = 'Município de Residência') %>%
  bold_labels()


的数据
问题是当我在RMarkdown上运行相同的命令时。它不按降序排列。我对其他表使用相同的命令,它工作得很好。
这是我从原始数据集得到的结果表:



但是有一行它的总(Total)值超过8k obs,它应该是表中的第一行。我不知道为什么会发生这种情况。

ztigrdn8

ztigrdn81#

问题中的第二个输出表显示,您正在使用.作为big.mark/千分频器,即最后一列“N = 9.150”。readr::parse_numbers()默认情况下期望,作为千分频器,并将.解析为十进制标记。

library(tidyverse)

parse_number("9.150")
#> [1] 9.15

字符串
我们可以通过在locale参数中将decimal_mark定义为,来改变这一点。

readr::parse_number("9.150", locale = readr::locale(decimal_mark = ","))
#> [1] 9150


我改变了你的例子数据,所以我们的总数超过1000,而里约热内卢将保持在1000以下。

data <- data.frame(
  anoobito = sample(2019:2023),
  NOME_MUNICIPIO = sample(
    c("Rio de Janeiro", "São Paulo", "Araruama", "Queimados"),
    230000,
    prob = c(.01, 1, 1, 1),
    replace = TRUE
  )
)

library(gtsummary)
theme_gtsummary_language("en", big.mark = ".", decimal.mark = ",")


如果我们使用您的原始代码,里约热内卢排在第一位,因为它的总价值被解析为高于其他地方的价值。

data %>%
  select(anoobito, NOME_MUNICIPIO) %>%
  tbl_summary(by = anoobito,
              label = c(NOME_MUNICIPIO ~ "")) %>%
  add_overall(last = T) %>%
  modify_table_body(fun = ~ arrange(., desc(parse_number(stat_0))))


x1c 0d1x的数据
当我们在parse_number()调用中包含locale = locale(decimal_mark = ",")时,我们得到了所需的行顺序。

data %>%
  select(anoobito, NOME_MUNICIPIO) %>%
  tbl_summary(by = anoobito,
              label = c(NOME_MUNICIPIO ~ "")) %>%
  add_overall(last = T) %>%
  modify_table_body(fun = ~ arrange(., desc(parse_number(
    stat_0, locale = locale(decimal_mark = ",")
  ))))


ct2axkht

ct2axkht2#

Till的解决方案很棒。这是另一个不使用modify_table_body()的版本,我有时觉得它更简单。编程愉快!

library(gtsummary)
#> #BlackLivesMatter
packageVersion("gtsummary")
#> [1] '1.7.2'
# set language to "pt"
theme_gtsummary_language("pt", big.mark = ".", decimal.mark = ",")
#> Setting theme `language: pt`

# simulate data
data <- 
  data.frame(
    anoobito = sample(2019:2023),
    NOME_MUNICIPIO = sample(c("Rio de Janeiro", "São Paulo", "Araruama", "Queimados"), 1000, replace = TRUE)
  )

tbl <-
  data |> 
  # build primary summary table
  tbl_summary(
    by = anoobito, 
    sort = list(NOME_MUNICIPIO = "frequency")
  ) |> 
  # add spanning header to the years
  modify_spanning_header(all_stat_cols() ~ "**Ano do Óbito**") |> 
  # add an overall column
  add_overall() |> 
  # udpate the headers, and put the Ns on a new line
  modify_header(label = '**Município de Residência**',
                all_stat_cols() ~ "**{level}**  \n N = {style_number(n)}") |> 
  # we don't want the header row, so remove it and undo the indentation
  remove_row_type(type = "header") |> 
  modify_table_styling(
    columns = label,
    text_format = "indent",
    undo_text_format = TRUE
  )

字符串
x1c 0d1x的数据
创建于2023-10-27带有reprex v2.0.2

相关问题