Using the gtsummary package, how can I sort rows in a grouped frequency table by one of the column?
Here is an example. I have a a dataset from a survey with a multichoice question. Each answer is represented by in a variable as dichotomous. I also have a factor variable and I want a breakdown of the frequencies by that grouping variable. I use tbl_summary to summarise. I also added an overall column of the frequencies. I would like to sort the rows by the Overall column or any other column for the different groups.
some_made_up_data <- data.frame(Q1a=c(rep(c(1,0,1,1,0),20)),
Q1b=c(rep(c(1,1,1,1,0),20)),
Q1c=c(rep(c(1,0,0,0,1),20)),
group=c(rep(c("g1","g2","g3","g4","g5"),20)))
some_made_up_data %>% tbl_summary(include = starts_with("Q1"),by=group) %>% add_overall()
I have tried to use the sort argument for tbl_summary()
but that doesn't work. I have also tried to transform to a gt table using the as.gt()
command but then could not sort table in gt
either.
Here is the only post I could find on this, but doesn't seem to help with my task.
How to sort or change rows order in a сharacteristic table column in the gtsummary package?
Is that even possible to do?