Numeric Values Formatting

format_value(x, ...)

# S3 method for data.frame
format_value(
  x,
  digits = 2,
  protect_integers = FALSE,
  missing = "",
  width = NULL,
  as_percent = FALSE,
  zap_small = FALSE,
  ...
)

# S3 method for numeric
format_value(
  x,
  digits = 2,
  protect_integers = FALSE,
  missing = "",
  width = NULL,
  as_percent = FALSE,
  zap_small = FALSE,
  ...
)

Arguments

x

Numeric value.

...

Arguments passed to or from other methods.

digits

Number of digits for rounding or significant figures. May also be "signif" to return significant figures or "scientific" to return scientific notation. Control the number of digits by adding the value as suffix, e.g. digits = "scientific4" to have scientific notation with 4 decimal places, or digits = "signif5" for 5 significant figures (see also signif()).

protect_integers

Should integers be kept as integers (i.e., without decimals)?

missing

Value by which NA values are replaced. By default, an empty string (i.e. "") is returned for NA.

width

Minimum width of the returned string. If not NULL and width is larger than the string's length, leading whitespaces are added to the string.

as_percent

Logical, if TRUE, value is formatted as percentage value.

zap_small

Logical, if TRUE, small values are rounded after digits decimal places. If FALSE, values with more decimal places than digits are printed in scientific notation.

Value

A formatted string.

Examples

format_value(1.20)
#> [1] "1.20"
format_value(1.2)
#> [1] "1.20"
format_value(1.2012313)
#> [1] "1.20"
format_value(c(0.0045, 234, -23))
#> [1] "4.50e-03" "234.00"   "-23.00"  
format_value(c(0.0045, .12, .34))
#> [1] "4.50e-03" "0.12"     "0.34"    
format_value(c(0.0045, .12, .34), as_percent = TRUE)
#> [1] "0.45%"  "12.00%" "34.00%"
format_value(c(0.0045, .12, .34), digits = "scientific")
#> [1] "4.50000e-03" "1.20000e-01" "3.40000e-01"
format_value(c(0.0045, .12, .34), digits = "scientific2")
#> [1] "4.50e-03" "1.20e-01" "3.40e-01"

# default
format_value(c(0.0045, .123, .345))
#> [1] "4.50e-03" "0.12"     "0.34"    
# significant figures
format_value(c(0.0045, .123, .345), digits = "signif")
#> [1] "0.0045" "0.123"  "0.345" 

format_value(as.factor(c("A", "B", "A")))
#> [1] A B A
#> Levels: A B
format_value(iris$Species)
#>   [1] setosa     setosa     setosa     setosa     setosa     setosa    
#>   [7] setosa     setosa     setosa     setosa     setosa     setosa    
#>  [13] setosa     setosa     setosa     setosa     setosa     setosa    
#>  [19] setosa     setosa     setosa     setosa     setosa     setosa    
#>  [25] setosa     setosa     setosa     setosa     setosa     setosa    
#>  [31] setosa     setosa     setosa     setosa     setosa     setosa    
#>  [37] setosa     setosa     setosa     setosa     setosa     setosa    
#>  [43] setosa     setosa     setosa     setosa     setosa     setosa    
#>  [49] setosa     setosa     versicolor versicolor versicolor versicolor
#>  [55] versicolor versicolor versicolor versicolor versicolor versicolor
#>  [61] versicolor versicolor versicolor versicolor versicolor versicolor
#>  [67] versicolor versicolor versicolor versicolor versicolor versicolor
#>  [73] versicolor versicolor versicolor versicolor versicolor versicolor
#>  [79] versicolor versicolor versicolor versicolor versicolor versicolor
#>  [85] versicolor versicolor versicolor versicolor versicolor versicolor
#>  [91] versicolor versicolor versicolor versicolor versicolor versicolor
#>  [97] versicolor versicolor versicolor versicolor virginica  virginica 
#> [103] virginica  virginica  virginica  virginica  virginica  virginica 
#> [109] virginica  virginica  virginica  virginica  virginica  virginica 
#> [115] virginica  virginica  virginica  virginica  virginica  virginica 
#> [121] virginica  virginica  virginica  virginica  virginica  virginica 
#> [127] virginica  virginica  virginica  virginica  virginica  virginica 
#> [133] virginica  virginica  virginica  virginica  virginica  virginica 
#> [139] virginica  virginica  virginica  virginica  virginica  virginica 
#> [145] virginica  virginica  virginica  virginica  virginica  virginica 
#> Levels: setosa versicolor virginica

format_value(3)
#> [1] "3.00"
format_value(3, protect_integers = TRUE)
#> [1] "3"

format_value(iris)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 1           5.10        3.50         1.40        0.20     setosa
#> 2           4.90        3.00         1.40        0.20     setosa
#> 3           4.70        3.20         1.30        0.20     setosa
#> 4           4.60        3.10         1.50        0.20     setosa
#> 5           5.00        3.60         1.40        0.20     setosa
#> 6           5.40        3.90         1.70        0.40     setosa
#> 7           4.60        3.40         1.40        0.30     setosa
#> 8           5.00        3.40         1.50        0.20     setosa
#> 9           4.40        2.90         1.40        0.20     setosa
#> 10          4.90        3.10         1.50        0.10     setosa
#> 11          5.40        3.70         1.50        0.20     setosa
#> 12          4.80        3.40         1.60        0.20     setosa
#> 13          4.80        3.00         1.40        0.10     setosa
#> 14          4.30        3.00         1.10        0.10     setosa
#> 15          5.80        4.00         1.20        0.20     setosa
#> 16          5.70        4.40         1.50        0.40     setosa
#> 17          5.40        3.90         1.30        0.40     setosa
#> 18          5.10        3.50         1.40        0.30     setosa
#> 19          5.70        3.80         1.70        0.30     setosa
#> 20          5.10        3.80         1.50        0.30     setosa
#> 21          5.40        3.40         1.70        0.20     setosa
#> 22          5.10        3.70         1.50        0.40     setosa
#> 23          4.60        3.60         1.00        0.20     setosa
#> 24          5.10        3.30         1.70        0.50     setosa
#> 25          4.80        3.40         1.90        0.20     setosa
#> 26          5.00        3.00         1.60        0.20     setosa
#> 27          5.00        3.40         1.60        0.40     setosa
#> 28          5.20        3.50         1.50        0.20     setosa
#> 29          5.20        3.40         1.40        0.20     setosa
#> 30          4.70        3.20         1.60        0.20     setosa
#> 31          4.80        3.10         1.60        0.20     setosa
#> 32          5.40        3.40         1.50        0.40     setosa
#> 33          5.20        4.10         1.50        0.10     setosa
#> 34          5.50        4.20         1.40        0.20     setosa
#> 35          4.90        3.10         1.50        0.20     setosa
#> 36          5.00        3.20         1.20        0.20     setosa
#> 37          5.50        3.50         1.30        0.20     setosa
#> 38          4.90        3.60         1.40        0.10     setosa
#> 39          4.40        3.00         1.30        0.20     setosa
#> 40          5.10        3.40         1.50        0.20     setosa
#> 41          5.00        3.50         1.30        0.30     setosa
#> 42          4.50        2.30         1.30        0.30     setosa
#> 43          4.40        3.20         1.30        0.20     setosa
#> 44          5.00        3.50         1.60        0.60     setosa
#> 45          5.10        3.80         1.90        0.40     setosa
#> 46          4.80        3.00         1.40        0.30     setosa
#> 47          5.10        3.80         1.60        0.20     setosa
#> 48          4.60        3.20         1.40        0.20     setosa
#> 49          5.30        3.70         1.50        0.20     setosa
#> 50          5.00        3.30         1.40        0.20     setosa
#> 51          7.00        3.20         4.70        1.40 versicolor
#> 52          6.40        3.20         4.50        1.50 versicolor
#> 53          6.90        3.10         4.90        1.50 versicolor
#> 54          5.50        2.30         4.00        1.30 versicolor
#> 55          6.50        2.80         4.60        1.50 versicolor
#> 56          5.70        2.80         4.50        1.30 versicolor
#> 57          6.30        3.30         4.70        1.60 versicolor
#> 58          4.90        2.40         3.30        1.00 versicolor
#> 59          6.60        2.90         4.60        1.30 versicolor
#> 60          5.20        2.70         3.90        1.40 versicolor
#> 61          5.00        2.00         3.50        1.00 versicolor
#> 62          5.90        3.00         4.20        1.50 versicolor
#> 63          6.00        2.20         4.00        1.00 versicolor
#> 64          6.10        2.90         4.70        1.40 versicolor
#> 65          5.60        2.90         3.60        1.30 versicolor
#> 66          6.70        3.10         4.40        1.40 versicolor
#> 67          5.60        3.00         4.50        1.50 versicolor
#> 68          5.80        2.70         4.10        1.00 versicolor
#> 69          6.20        2.20         4.50        1.50 versicolor
#> 70          5.60        2.50         3.90        1.10 versicolor
#> 71          5.90        3.20         4.80        1.80 versicolor
#> 72          6.10        2.80         4.00        1.30 versicolor
#> 73          6.30        2.50         4.90        1.50 versicolor
#> 74          6.10        2.80         4.70        1.20 versicolor
#> 75          6.40        2.90         4.30        1.30 versicolor
#> 76          6.60        3.00         4.40        1.40 versicolor
#> 77          6.80        2.80         4.80        1.40 versicolor
#> 78          6.70        3.00         5.00        1.70 versicolor
#> 79          6.00        2.90         4.50        1.50 versicolor
#> 80          5.70        2.60         3.50        1.00 versicolor
#> 81          5.50        2.40         3.80        1.10 versicolor
#> 82          5.50        2.40         3.70        1.00 versicolor
#> 83          5.80        2.70         3.90        1.20 versicolor
#> 84          6.00        2.70         5.10        1.60 versicolor
#> 85          5.40        3.00         4.50        1.50 versicolor
#> 86          6.00        3.40         4.50        1.60 versicolor
#> 87          6.70        3.10         4.70        1.50 versicolor
#> 88          6.30        2.30         4.40        1.30 versicolor
#> 89          5.60        3.00         4.10        1.30 versicolor
#> 90          5.50        2.50         4.00        1.30 versicolor
#> 91          5.50        2.60         4.40        1.20 versicolor
#> 92          6.10        3.00         4.60        1.40 versicolor
#> 93          5.80        2.60         4.00        1.20 versicolor
#> 94          5.00        2.30         3.30        1.00 versicolor
#> 95          5.60        2.70         4.20        1.30 versicolor
#> 96          5.70        3.00         4.20        1.20 versicolor
#> 97          5.70        2.90         4.20        1.30 versicolor
#> 98          6.20        2.90         4.30        1.30 versicolor
#> 99          5.10        2.50         3.00        1.10 versicolor
#> 100         5.70        2.80         4.10        1.30 versicolor
#> 101         6.30        3.30         6.00        2.50  virginica
#> 102         5.80        2.70         5.10        1.90  virginica
#> 103         7.10        3.00         5.90        2.10  virginica
#> 104         6.30        2.90         5.60        1.80  virginica
#> 105         6.50        3.00         5.80        2.20  virginica
#> 106         7.60        3.00         6.60        2.10  virginica
#> 107         4.90        2.50         4.50        1.70  virginica
#> 108         7.30        2.90         6.30        1.80  virginica
#> 109         6.70        2.50         5.80        1.80  virginica
#> 110         7.20        3.60         6.10        2.50  virginica
#> 111         6.50        3.20         5.10        2.00  virginica
#> 112         6.40        2.70         5.30        1.90  virginica
#> 113         6.80        3.00         5.50        2.10  virginica
#> 114         5.70        2.50         5.00        2.00  virginica
#> 115         5.80        2.80         5.10        2.40  virginica
#> 116         6.40        3.20         5.30        2.30  virginica
#> 117         6.50        3.00         5.50        1.80  virginica
#> 118         7.70        3.80         6.70        2.20  virginica
#> 119         7.70        2.60         6.90        2.30  virginica
#> 120         6.00        2.20         5.00        1.50  virginica
#> 121         6.90        3.20         5.70        2.30  virginica
#> 122         5.60        2.80         4.90        2.00  virginica
#> 123         7.70        2.80         6.70        2.00  virginica
#> 124         6.30        2.70         4.90        1.80  virginica
#> 125         6.70        3.30         5.70        2.10  virginica
#> 126         7.20        3.20         6.00        1.80  virginica
#> 127         6.20        2.80         4.80        1.80  virginica
#> 128         6.10        3.00         4.90        1.80  virginica
#> 129         6.40        2.80         5.60        2.10  virginica
#> 130         7.20        3.00         5.80        1.60  virginica
#> 131         7.40        2.80         6.10        1.90  virginica
#> 132         7.90        3.80         6.40        2.00  virginica
#> 133         6.40        2.80         5.60        2.20  virginica
#> 134         6.30        2.80         5.10        1.50  virginica
#> 135         6.10        2.60         5.60        1.40  virginica
#> 136         7.70        3.00         6.10        2.30  virginica
#> 137         6.30        3.40         5.60        2.40  virginica
#> 138         6.40        3.10         5.50        1.80  virginica
#> 139         6.00        3.00         4.80        1.80  virginica
#> 140         6.90        3.10         5.40        2.10  virginica
#> 141         6.70        3.10         5.60        2.40  virginica
#> 142         6.90        3.10         5.10        2.30  virginica
#> 143         5.80        2.70         5.10        1.90  virginica
#> 144         6.80        3.20         5.90        2.30  virginica
#> 145         6.70        3.30         5.70        2.50  virginica
#> 146         6.70        3.00         5.20        2.30  virginica
#> 147         6.30        2.50         5.00        1.90  virginica
#> 148         6.50        3.00         5.20        2.00  virginica
#> 149         6.20        3.40         5.40        2.30  virginica
#> 150         5.90        3.00         5.10        1.80  virginica