Skip to contents

format_ functions modify an existing variable for aesthetic reasons.

format_digit

The goal of format_digit is to add zero(s) on the left of a number.

x <- c(1,4,10,12,100,2000)

format_digit(x)
#> [1] "01"   "04"   "10"   "12"   "100"  "2000"

You can also set the argument digits to add more zeros.

format_digit(x,digits = 4)
#> [1] "0001" "0004" "0010" "0012" "0100" "2000"

format_num

The goal of format_num is to add markers to a number.

format_num(12345.67)
#> [1] "12,345.67"

You can also change the markers to suit other characters.

format_num(12345.67,decimal_mark = "*",thousand_mark = "#")
#> [1] "12#345*67"

Also, if you’re a Brazilian like me, I made a case for making our lives easier.

format_num(12345.67,br_mark = TRUE)
#> [1] "12.345,67"

format_scale

The goal of format_scale is to reescale a variable, given by:

\[y_i = (y_{\max} - y_{\min}) * \left[\frac{x_i - \min(x_i)}{\max(x)-\min(x)} \right] + y_{\min},\] where:

  • \(y_i\) is the reescaled numeric vector;
  • \(y_{\max}\) is the new maximum value, after the reescale;
  • \(y_{\min}\) is the new minimum value, after the reescale;
  • \(x_i\) is the original numeric vector;
  • \(\min(x_i)\) is the original minimum value of \(x_i\);
  • \(\max(x_i)\) is the original maximum value of \(x_i\).

By default, the vector will be rescaled with new minimum and maximum values of 0 and 1.

x <- seq(-3,3,l = 10)

x
#>  [1] -3.0000000 -2.3333333 -1.6666667 -1.0000000 -0.3333333  0.3333333
#>  [7]  1.0000000  1.6666667  2.3333333  3.0000000

y <- format_scale(x)

y
#>  [1] 0.0000000 0.1111111 0.2222222 0.3333333 0.4444444 0.5555556 0.6666667
#>  [8] 0.7777778 0.8888889 1.0000000

You can also change the range of the new scale.

z <- format_scale(x,new_min = 25,new_max = 100)

format_p_value

The goal of format_p_value is to change a p value, by considering a lower bound value where if is the value is smaller than it, let’s say 0.001, the p value will be changed to “<0.001

format_p_value(c(.001,.00000001),lower_bound = 0.001)
#> [1] "0.0010" "<0.001"