Sum of a column in tbl_summary?

Advertisements

I’m trying to create a table using tbl_summary() that contains sums of columns (a sum of the correct test scores and incorrect test scores), however it seems to keep treating my continuous variables as categorical?

I have tried specifying the type as continuous with no luck.

What I’m aiming for:

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

I’ve tried the below:

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}
  )

Produces a count of each result as below:

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)

Produces an error "Error: Error processing statistic argument for element ‘Anatomy_yes’. Expecting a character as the passed value."

>Solution :

You almost made it. Check the documentation again.The type option since the default for numeric values less than 10 is categorical.

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")
  )

Is this what you like to achieve?

With statistic option, specified for the sum:

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

Leave a ReplyCancel reply