f_conditional_round {rfriend} | R Documentation |
Conditional Rounding for Numeric Values
Description
Conditionally formats numeric values based on their magnitude. Values that are very small or very large are formatted using scientific notation, while other values are rounded to a specified number of decimal places. Integers are preserved without decimal places. When applied to a data frame, only numeric columns are processed. All output is character string.
Usage
f_conditional_round(
x,
threshold_small = 0.01,
threshold_large = 9999,
digits = 3,
replace_na = TRUE,
detect_int_col = TRUE
)
Arguments
x |
A numeric vector or data frame containing numeric columns to be formatted. |
threshold_small |
Numeric value. Values with absolute magnitude smaller than this
threshold will be formatted using scientific notation. Default is |
threshold_large |
Numeric value. Values with absolute magnitude larger than or equal
to this threshold will be formatted using scientific notation. Default is |
digits |
Integer. Number of significant digits to use in formatting. Default is |
replace_na |
Logical. If TRUE, NA values will be replaced with empty strings ("") in the output. Default is TRUE. |
detect_int_col |
Logical. If |
Details
The function applies the following formatting rules:
Values smaller than
threshold_small
or larger thanthreshold_large
are formatted in scientific notation withdigits
significant digits.Integer values are formatted without decimal places.
Non-integer values that don't require scientific notation are rounded to
digits
decimal places.NA values are replaced with empty strings if
replace_na = TRUE
.Empty strings in the input are preserved.
For data frames, only numeric columns are processed; other columns remain unchanged.
Value
If input is a vector: A character vector of the same length as the input, with values formatted according to the specified rules.
If input is a data frame: A data frame with the same structure as the input, but with character columns formatted according to the specified rules.
Author(s)
Sander H. van Delden plantmind@proton.me
Examples
# Vector examples.
f_conditional_round(c(0.0001, 0.5, 3, 10000))
# Returns: "1.000e-04" "0.500" "3" "1.000e+04".
f_conditional_round(c(0.0001, 0.5, 3, 10000, NA), replace_na = TRUE)
# Returns: "1.000e-04" "0.500" "3" "1.000e+04" ""
# Data frame example.
df <- data.frame(
name = c("A", "B", "C"),
small_val = c(0.0001, 0.002, 0.5),
integer = c(1, 2, 3),
integer_mix = c(10, 20, 30.1),
large_val = c(10000, 5000, NA)
)
# Show only two digits.
f_conditional_round(df, digits = 2)
# To keep Integers as Integers (no digits)
# in columns with mixed data (Integers and digits)
# set detect_int_col = FALSE
f_conditional_round(df, detect_int_col = FALSE)