Reputation: 45
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)
Upvotes: 2
Views: 937
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
Upvotes: 2