Function formats numeric columns only. Other classes are left intact.

format_numbers(data, digits = 3, format = "f", fun = formatC, ...)

Arguments

data

A data frame.

digits

(numeric or NA) a desired number of digits after the decimal point (if format = "f") or a number of significant digits (format = "g", = "e" or = "fg").
Either one integer (to set the same formatting for all columns) or a vector of inegers (to set formatting for each column separately).
Use NA to leave column unformatted.

format

(character) "f", "g", "e", "fg". Either one value or a vector of values for each column. Each value will be passed to fun separately.
"f" gives numbers in the usual xxx.xxx format;
"e" and "E" give n.ddde+nn or n.dddE+nn (scientific format);
"g" and "G" put number into scientific format only if it saves space to do so.
"fg" uses fixed format as "f", but digits as the minimum number of significant digits. This can lead to quite long result strings

fun

A function that does the formatting. Default is base::formatC().

...

Other parameters to be passed to fun.

Examples

library(biostat) DATA <- head(iris) # The same rounding for each column format_numbers(DATA, 2)
#> 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
# Different rounding for different columns format_numbers(DATA, c(2, 2, 3, 3, NA))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.10 3.50 1.400 0.200 setosa #> 2 4.90 3.00 1.400 0.200 setosa #> 3 4.70 3.20 1.300 0.200 setosa #> 4 4.60 3.10 1.500 0.200 setosa #> 5 5.00 3.60 1.400 0.200 setosa #> 6 5.40 3.90 1.700 0.400 setosa
# Use `NA` to leave a column unformatted format_numbers(DATA, c(4, NA, 3, NA, NA))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.1000 3.5 1.400 0.2 setosa #> 2 4.9000 3.0 1.400 0.2 setosa #> 3 4.7000 3.2 1.300 0.2 setosa #> 4 4.6000 3.1 1.500 0.2 setosa #> 5 5.0000 3.6 1.400 0.2 setosa #> 6 5.4000 3.9 1.700 0.4 setosa