A family of functions for formatting numbers and then padding with spaces so that table columns can be both centered and decimal aligned.

pad_counts(x, digits = 0L)

pad_prop(x, digits, fmt_small = TRUE, keep_zero = FALSE, output = NULL)

pad_corr(x, digits, output = NULL)

pad_decimal(
  x,
  digits,
  fmt_small = FALSE,
  max_value = NULL,
  keep_zero = FALSE,
  output = NULL
)

Arguments

x

Number or number string to be formatted

digits

Number of decimal places to retain

fmt_small

Indicator for replacing zero with < (e.g., .000 becomes < .001). Default is TRUE.

keep_zero

If fmt_small is TRUE, whether to preserve true 0s (e.g., 0.0000001 becomes <.001, but 0.0000000 stays .000).

output

The output type for the rendered document. One of "latex" or "html".

max_value

If fmt_small is TRUE and a max_value is supplied, any value greater than the max_value is replaced with > (e.g., if max_value = 50, then 60 becomes >49.9). The number of digits depends on digits.

Value

A character vector of the same length as x.

Details

pad_counts should be used to pad integer numbers. This wraps base::format() to add a comma separator.

pad_prop should be used to pad decimal numbers between [0,1]. This wraps fmt_prop() to round to a specified number of digits and optionally remove the leading zero.

pad_corr should be used to pad decimal numbers between [-1,1]. This wraps fmt_corr(), and is similar to pad_prop, but accounts for negative numbers when adding padding.

pad_decimal should be used to pad decimal number that are not bounded. This wraps fmt_digits() to round to a specified number of decimal places.

See also

Other formatters: fmt_table(), formatting

Examples

pad_counts(sample(1:1000, size = 20))
#>  [1] "115" "224" "858" "463" "751" "412" "672" "653" "938" "579" "617" "980"
#> [13] "979" "411" "565" "786" "583" "733" "220" "502"

pad_prop(c(0.001, runif(5)), digits = 2)
#> [1] "<.01\\ \\ \\ " ".84"           ".02"           ".82"          
#> [5] ".54"           ".48"          

pad_corr(runif(10, -1, 1), digits = 2)
#>  [1] ".10"              "&minus;.76\\ \\ " "&minus;.05\\ \\ " "&minus;.11\\ \\ "
#>  [5] ".08"              ".59"              "&minus;.11\\ \\ " "&minus;.97\\ \\ "
#>  [9] ".01"              "&minus;.68\\ \\ "

pad_decimal(runif(10, 1, 100), digits = 1)
#>  [1] "53.5"      "36.5"      "\\ \\ 3.7" "25.6"      "12.1"      "34.7"     
#>  [7] "14.6"      "98.0"      "94.8"      "69.3"