Hello, thanks in advance for your help! New R user, I’ve tried reading the websites for tbl_summary, tabyl, handbook, and other online forums but haven’t found an answer.
I have created a table of screening criteria and cases who meet them. I want to be able to arrange both columns and rows differently. I would like the “Yes” column to appear to the left of the “No” column. I’d also like to arrange the rows in order from fewest % Yes to greatest % Yes. I’m hoping to finish this up in the next week or so, but it is not urgent. Output & Reprex below!
# Reprex of screening criteria table
# Load packages -----------------------------------------------------------
pacman::p_load(
rio, # for importing data
here, # for locating files
skimr, # for reviewing the data
janitor, # for data cleaning
officer, # border lines
epikit, # creating age categories
gtsummary, # creating tables
RColorBrewer, # for color palettes
viridis, # for more color palettes
scales, # percents in tables
flextable, # for making pretty tables
officer, # helper functions for nice tables
tidyverse, # for data management and visualization
reprex,
datapasta
)
# Import data -------------------------------------------------------------
# import case data
review_raw <- data.frame(
stringsAsFactors = FALSE,
oos_ovnight_past12 = c("Y", "N", "N", "N", "Y"),
int_hemo_past12 = c("N", "N", "N", "N", "N"),
int_op_surgery_past12 = c("N", "N", "N", "N", "N"),
ooowi_ovnight_past12 = c("Y", "N", "N", "N", "Y"),
int_ovnight_past12 = c("Y", "N", "N", "N", "Y"),
us_burden_ovnight_past12 = c("Y", "N", "N", "N", "N"),
invasive_device = c("N", "N", "N", "N", "N"),
chronic_wound = c("Y", "N", "N", "N", "N")
)
# Screening criteria ------------------------------------------------------
# create a new data frame by pivoting the screening criteria
criteria_long <- review_raw %>%
# select columns to include
select(oos_ovnight_past12:chronic_wound) %>%
# select columns to pivot, : selects range between these two variables
pivot_longer(cols = oos_ovnight_past12:chronic_wound)
# create a facor of screening criteria
criteria_factor <- criteria_long %>%
# sort by name of criteria
arrange(name, value) %>%
# attempting to somehow re-order the criteria names, but it doesn't work?
mutate(criteria = factor(name, unique(name)))
# use newly created data frame
criteria_factor %>%
# create a table
tabyl(criteria,value) %>%
# display percentages
adorn_percentages() %>%
# format percentages to whole numbers
adorn_pct_formatting(digits = 0) %>%
# add n values after percentages
adorn_ns("rear") %>%
qflextable()
[image]
Created on 2024-05-20 with reprex v2.1.0
Session info
#> R version 4.3.3 (2024-02-29 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19044)