Negates and swaps confidence interval bounds. Takes a CI string in the format "(lower%, upper%)" and returns "(-upper%, -lower%)". This is useful when gtsummary::add_difference_row() computes reference - arm but you need arm - reference.

reverse_ci(x)

Arguments

x

(character)
A character vector of confidence interval strings in format "(lower%, upper%)".

Value

A character vector with negated and swapped CI bounds.

See also

reverse_rate_difference() for reversing rate difference values.

Examples

# Basic usage - negates values and swaps order
reverse_ci(c("(2.5%, 10.0%)", "(-5.0%, 3.0%)"))
#> [1] "(-10.0%, -2.5%)" "(-3.0%, 5.0%)"  

# Handles NA and empty strings
reverse_ci(c("(1.0%, 5.0%)", NA, ""))
#> [1] "(-5.0%, -1.0%)" NA               NA              

# Handles negative bounds
reverse_ci("(-8.0%, -2.0%)")
#> [1] "(2.0%, 8.0%)"

# Example: Reversing direction in a gtsummary table
# When add_difference_row() computes "reference - arm" but you need "arm - reference"
library(gtsummary)

tbl <- trial |>
  tbl_summary(
    by = trt,
    include = response,
    missing = "no"
  ) |>
  add_difference_row(
    include = response,
    reference = "Drug A",
    statistic = response ~ c("{estimate}", "({conf.low}, {conf.high})"),
    estimate_fun = response ~ label_style_number(digits = 1, scale = 100, suffix = "%")
  )

# Reverse the direction using modify_table_body
tbl |>
  modify_table_body(
    ~ .x |>
      dplyr::mutate(
        dplyr::across(
          dplyr::starts_with("stat_"),
          ~ ifelse(variable == "response-row_difference" & label == "Rate Difference",
            reverse_rate_difference(.x), .x
          )
        )
      ) |>
      dplyr::mutate(
        dplyr::across(
          dplyr::starts_with("stat_"),
          ~ ifelse(variable == "response-row_difference" &
            label == "(CI Lower Bound, CI Upper Bound)",
          reverse_ci(.x), .x
          )
        )
      )
  )
#> Warning: There was 1 warning in `dplyr::mutate()`.
#>  In argument: `dplyr::across(...)`.
#> Caused by warning in `FUN()`:
#> ! NAs introduced by coercion
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
Tumor Response 28 (29%) 33 (34%)
    Rate Difference 4.2%
    (CI Lower Bound, CI Upper Bound) (-9.9%, 18.3%)
1 n (%)