tbl_summary中某列的总和?

um6iljoc  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(158)

我尝试使用tbl_summary()创建一个包含列总和的表(正确测试分数和错误测试分数的总和),但是它似乎一直将我的连续变量视为分类变量?
我尝试将类型指定为continuous,但没有成功。
我的目标是:

library(gtsummary)
library(tidyverse)

test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
                   "correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
                   "incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))

output <- test %>%
  group_by(With_assistant) %>%
  summarize(
    total_correct=sum(correct_answers, na.rm=TRUE), 
    total_incorrect=(sum(incorrect_answers, na.rm=TRUE))
  ) 
output

Table_1
我尝试了以下方法:

library(gtsummary)
library(tidyverse)

test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
                   "correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
                   "incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))
output <- test %>%
  tbl_summary(
    by = With_assistant,
    statistic = all_continuous() ~ {n}
  )

生成每个结果的计数,如下所示:
Table_2

library(gtsummary)
library(tidyverse)

test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
                   "correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
                   "incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))

output <- test %>%
  tbl_summary(
    by = With_assistant,
    type = c(correct_answers, incorrect_answers) ~ "continuous",
    statistic = all_continuous() ~ {n},
    percent = "column",
    missing = "no"
  ) %>%
  print(output)

产生错误“* 错误:处理元素“Anatomy_yes”的statistic参数时出错。传递的值应为字符。*”

qni6mghb

qni6mghb1#

您几乎成功了。再次查看文档。type选项,因为小于10的数值的默认值是分类的。

library(gtsummary)
library(tidyverse)

test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
                   "correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
                   "incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))

test %>%
  tbl_summary(
    by = With_assistant,
    type = list(c(correct_answers, incorrect_answers) ~ "continuous")
  )

这就是你想达到的目标吗?

使用statistic选项,为总和指定:

test %>%
  tbl_summary(
    by = With_assistant,
    type = list(c(correct_answers, incorrect_answers) ~ "continuous"),
    statistic = list(c(correct_answers, incorrect_answers) ~ "{sum}")
  )

相关问题