cols_merge: Add NA handling with custom pattern #1010
-
Hi there, I would like to create an ordinary clinical summary table with columns for treatments, summarizing descriptive stats or model outputs. In my reprex, I would like to create a table with columns for label and treatment, where treatment contains the number of subjects n, some mean value, a CI, formatted as "[lower_bound; upper_bound]" and Min and Max, formatted as "Min - Max". I achieved my goal for Min and Max using
Omitting the pattern argument in Merging all remaining trt columns would then lead to |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi Alex, Sorry about the difficulty encountered here. Right now the library(gt)
library(tidyverse)
tbl <- tibble(label = c("n" ,"Mean", "95% CI", "Min - Max"),
trt_n = c(15, 14, rep(NA, 2)),
trt_ci_low = c(rep(NA, 2), 10, NA),
trt_ci_up = c(rep(NA, 2), 18, NA),
trt_min = c(rep(NA, 3), 4),
trt_max = c(rep(NA, 3), 20))
tbl %>%
gt() %>%
sub_missing(missing_text = "") %>%
text_transform(
locations = cells_body(columns = trt_ci_low),
fn = function(x) {
out <- rep(NA_character_, length(x))
for (i in seq_along(out)) {
if (!is.na(x[i]) && !is.na(tbl$trt_ci_up[i])) {
out[i] <- paste0(x[i], "; ", tbl$trt_ci_up[i])
} else {
out[i] <- ""
}
}
out
}
) %>%
cols_hide(columns = trt_ci_up) %>%
cols_merge_range(col_begin = "trt_min", col_end = "trt_max", sep = " - ") The Not at all an ideal solution but I hope it can be used as a workaround until the merging functions become more feature complete. |
Beta Was this translation helpful? Give feedback.
Hi Alex,
Sorry about the difficulty encountered here. Right now the
pattern
argument doesn't allow for more expressive arrangements of text that account forNA
values (this is something that is on my radar). However, we can perform a more manual solution withtext_transform()
. Here is what I wrote: