The sub_small_vals() function

Let’s generate a simple, single-column tibble that contains an assortment of values that could potentially undergo some substitution.

tbl <- dplyr::tibble(num = c(10^(-4:2), 0, NA))

tbl
# A tibble: 9 × 1
       num
     <dbl>
1   0.0001
2   0.001 
3   0.01  
4   0.1   
5   1     
6  10     
7 100     
8   0     
9  NA     

The tbl contains a variety of smaller numbers and some might be small enough to reformat with a threshold value. With sub_small_vals() we can do just that:

tbl |>
  gt() |>
  fmt_number(columns = num) |>
  sub_small_vals()
num
<0.01
<0.01
0.01
0.10
1.00
10.00
100.00
0.00
NA

Small and negative values can also be handled but they are handled specially by the sign parameter. Setting that to "-" will format only the small, negative values.

tbl |>
  dplyr::mutate(num = -num) |>
  gt() |>
  fmt_number(columns = num) |>
  sub_small_vals(sign = "-")
num
<abs(-0.01)
<abs(-0.01)
−0.01
−0.10
−1.00
−10.00
−100.00
0.00
NA

You don’t have to settle with the default threshold value or the default replacement pattern (in small_pattern). This can be changed and the "{x“} in small_pattern (which uses the threshold value) can even be omitted.

tbl |>
  gt() |>
  fmt_number(columns = num) |>
  sub_small_vals(
    threshold = 0.0005,
    small_pattern = "smol"
  )
num
smol
0.00
0.01
0.10
1.00
10.00
100.00
0.00
NA