Marrrrie
Marrrrie

Reputation: 45

gtsummary::tbl_summary and unique variable values

I am trying to use gtsummary::tbl_summary to create a table that allows me to use distinct values within variables. I have a dataframe where each row represents a patient visit meaning that an ID can be present more than once. Instead of having each ID count listed in the table, I want a count of the sum of unique IDs. Trying to stick with gtsummary since it provides nicely formatted values

library(gtsummary)

df <- data.frame(id   = c("a", "b", "c", "a", "a", "c", "b"))
tbl_summary(df)

Current output

Desired output

Upvotes: 2

Views: 937

Answers (1)

akrun
akrun

Reputation: 886938

Based on the desired, we may get the n_distinct values from 'id'

library(gtsummary)
library(dplyr)
library(tidyr)
df %>% 
  summarise(Number_of_Patients = n_distinct(id)) %>%
  uncount(Number_of_Patients, .remove = FALSE) %>%
  tbl_summary()

-output

enter image description here

Upvotes: 2

Related Questions