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

Sum of a column in tbl_summary?

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:

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

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?

enter image description here

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

enter image description here

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