format_value()
converts numeric values into formatted string values, where
formatting can be something like rounding digits, scientific notation etc.
format_percent()
is a short-cut for format_value(as_percent = TRUE)
.
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,
lead_zero = TRUE,
style_positive = "none",
style_negative = "hyphen",
decimal_point = getOption("OutDec"),
...
)
# S3 method for numeric
format_value(
x,
digits = 2,
protect_integers = FALSE,
missing = "",
width = NULL,
as_percent = FALSE,
zap_small = FALSE,
lead_zero = TRUE,
style_positive = "none",
style_negative = "hyphen",
decimal_point = getOption("OutDec"),
...
)
format_percent(x, ...)
A formatted string.
Numeric value.
Arguments passed to or from other methods.
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()
).
Should integers be kept as integers (i.e., without decimals)?
Value by which NA
values are replaced. By default, an
empty string (i.e. ""
) is returned for NA
.
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.
Logical, if TRUE
, value is formatted as percentage
value.
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.
Logical, if TRUE
(default), includes leading zeros, else
leading zeros are dropped.
A string that determines the style of positive numbers.
May be "none"
(default), "plus"
to add a plus-sign or "space"
to
precede the string by a Unicode "figure space", i.e., a space equally as
wide as a number or +
.
A string that determines the style of negative numbers.
May be "hyphen"
(default), "minus"
for a proper Unicode minus symbol or
"parens"
to wrap the number in parentheses.
Character string containing a single character that is used as decimal point in output conversions.
format_value(1.20)
format_value(1.2)
format_value(1.2012313)
format_value(c(0.0045, 234, -23))
format_value(c(0.0045, 0.12, 0.34))
format_value(c(0.0045, 0.12, 0.34), as_percent = TRUE)
format_value(c(0.0045, 0.12, 0.34), digits = "scientific")
format_value(c(0.0045, 0.12, 0.34), digits = "scientific2")
format_value(c(0.045, 0.12, 0.34), lead_zero = FALSE)
format_value(c(0.0045, 0.12, 0.34), decimal_point = ",")
# default
format_value(c(0.0045, 0.123, 0.345))
# significant figures
format_value(c(0.0045, 0.123, 0.345), digits = "signif")
format_value(as.factor(c("A", "B", "A")))
format_value(iris$Species)
format_value(3)
format_value(3, protect_integers = TRUE)
format_value(head(iris))
Run the code above in your browser using DataLab