View source: R/restore-zeros.R
restore_zeros | R Documentation |
restore_zeros()
takes a vector with values that might have
lost trailing zeros, most likely from being registered as numeric. It turns
each value into a string and adds trailing zeros until the mantissa hits
some limit.
The default for that limit is the number of digits in the longest mantissa of the vector's values. The length of the integer part plays no role.
Don't rely on the default limit without checking: The original width could have been larger because the longest extant mantissa might itself have lost trailing zeros.
restore_zeros_df()
is a variant for data frames. It wraps
restore_zeros()
and, by default, applies it to all columns that are
coercible to numeric.
restore_zeros(x, width = NULL, sep_in = "\\.", sep_out = sep_in)
restore_zeros_df(
data,
cols = everything(),
check_numeric_like = TRUE,
check_decimals = FALSE,
width = NULL,
sep_in = "\\.",
sep_out = NULL,
...
)
x |
Numeric (or string coercible to numeric). Vector of numbers that might have lost trailing zeros. |
width |
Integer. Number of decimal places the mantissas should have,
including the restored zeros. Default is |
sep_in |
Substring that separates the input's mantissa from its integer
part. Default is |
sep_out |
Substring that will be returned in the output to separate the
mantissa from the integer part. By default, |
data |
Data frame or matrix. Only in |
cols |
Only in |
check_numeric_like |
Logical. Only in |
check_decimals |
Logical. Only in |
... |
Only in |
These functions exploit the fact that groups of summary values such
as means or percentages are often reported to the same number of decimal
places. If such a number is known but values were not entered as strings,
trailing zeros will be lost. In this case, restore_zeros()
or
restore_zeros_df()
will be helpful to prepare data for consistency
testing functions such as grim_map()
or grimmer_map()
.
For restore_zeros()
, a string vector. At least some of the strings
will have newly restored zeros, unless (1) all input values had the same
number of decimal places, and (2) width
was not specified as a number
greater than that single number of decimal places.
For restore_zeros_df()
, a data frame.
You might not see all decimal places of
numeric values in a vector, and consequently wonder if restore_zeros()
,
when applied to the vector, adds too many zeros. That is because displayed
numbers, unlike stored numbers, are often rounded.
For a vector x
, you can count the characters of the longest mantissa from
among its values like this:
x %>% decimal_places() %>% max()
Wrapped functions: sprintf()
.
# By default, the target width is that of
# the longest mantissa:
vec <- c(212, 75.38, 4.9625)
vec %>%
restore_zeros()
# Alternatively, supply a number via `width`:
vec %>%
restore_zeros(width = 6)
# For better printing:
iris <- tibble::as_tibble(iris)
# Apply `restore_zeros()` to all numeric
# columns, but not to the factor column:
iris %>%
restore_zeros_df()
# Select columns as in `dplyr::select()`:
iris %>%
restore_zeros_df(starts_with("Sepal"), width = 3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.