如何将 tbl_summary 表中计算出的 0(0%) 更改为 NA

问题描述 投票:0回答:1

我正在使用 tbl_summary 为一个研究项目制作人口统计表。所描述的变量之一仅适用于案例而不适用于控件,但我无论如何都希望将其列在表中。对照的抗体都有“否”值,而病例可以有“是”或“否”。我遇到的问题是我想用 NA 替换“对照/是”框中的“0”。有没有办法获取 tbl_summary 表中的这个特定位置?

tbl_summary(
  data,
  by = group, missing='no',
  type = all_dichotomous() ~ "categorical",
  statistic = list(
    all_categorical() ~ "{n} ({p}%)",
    all_continuous() ~ "{mean} ({sd})"
  ),
    include = c('age', 'sex', 'n_observations','antibodies'),
) %>%
  add_p(pvalue_fun = function(x) style_pvalue(x, digits = 2), include=-antibodies) %>%
  modify_header(p.value = ("**P Value**"))%>%
  add_overall() %>%
  modify_caption("**Table 1** (N = {N})")
特点 整体 案例(N=85) 控制(N=85) P值
77(45%) 43(52%) 34(40%) 0.17
93(55%) 42(49%) 52(60%)
检测到抗体
没有 140(82%) 55(65%) 85(100%)
是的 30 (18%) 30 (35%) 0 (0%)

我希望将 Controls/Yes 框替换为 NA。

我还想从抗体中删除整个列。但这只是较大表格的一小部分,我不知道如何在不完全删除它的情况下做到这一点。

r gtsummary
1个回答
0
投票

将tbl_summary的结果保存到R对象中,然后修改$table_body属性的单元格。由于您没有提供任何数据,我将使用 MASS 包中的调查数据集给出一个示例。

library(MASS)
data(survey)
head(survey)

A <- survey |> tbl_summary(
  by = Sex, missing='no',
  type = all_dichotomous() ~ "categorical",
  statistic = list(
    all_categorical() ~ "{n} ({p}%)",
    all_continuous() ~ "{mean} ({sd})"
  ),
  include = c('Clap', 'Fold')) %>%
  add_p(pvalue_fun = function(x) style_pvalue(x, digits = 2)) %>%
  modify_header(p.value = ("**P Value**"))%>%
  add_overall() %>%
  modify_caption("**Table 1** (N = {N})"); A

A 有属性

attributes(A)
$names
[1] "table_body"    "table_styling" "meta_data"     "inputs"        "N"
[6] "call_list"     "by"            "df_by"

$class
[1] "tbl_summary" "gtsummary"

$table_body
包含数据。

A$table_body
# A tibble: 4 × 13

  variable test_name           var_type var_label row_type label stat_0 stat_1 stat_2
  <chr>    <chr>               <chr>    <chr>     <chr>    <chr> <chr>  <chr>  <chr> 
1 Fold     chisq.test.no.corr… categor… Fold      label    Fold  NA     NA     NA    
2 Fold     chisq.test.no.corr… categor… Fold      level    L on… 98 (4… 48 (4… 50 (4…
3 Fold     chisq.test.no.corr… categor… Fold      level    Neit… 18 (7… 6 (5.… 12 (1…
4 Fold     chisq.test.no.corr… categor… Fold      level    R on… 120 (… 64 (5… 56 (4…
# ℹ 4 more variables: test_result <list>, statistic <dbl>, parameter <int>,
#   p.value <dbl>

将中间单元格“两者都不是:女性 [6 (5.1%)]”替换为“NA”:

A$table_body <- mutate(A$table_body, stat_1=ifelse(variable=="Fold" & label=="Neither", "NA", stat_1))
A

第二个问题也可以用同样的方法。

detach(package:MASS)
© www.soinside.com 2019 - 2024. All rights reserved.