Skip to contents

Round numbers to a specified minimum number of significant digits while ensuring the integer part is not rounded. This prevents values from ending in rounded zeros, e.g., 14300 instead of 14286 when rounding 1e5 / 7 to three significant digits.

Usage

signif_custom(x, digits = 3L, type = c("selective", "expanded"))

Arguments

x

numeric vector, matrix or data.frame, see Details.

digits

the minimum number of significant digits to round to (see Details) or Inf to not round values.

type

character string "selective" or "expanded" indicating the type of rounding to be used, see Details.

Value

x with values rounded according to digits and type.

Details

Infinite values are returned unchanged. NA_integer_ and NA_real_ are returned as NA_real_. matrices and dataframes are handled by rounding values of numeric columns. Non-numeric (e.g., factor) x is not supported, use as.numeric_safe() beforehand on x if appropriate.

Numeric values are rounded according to arguments digits and type. Values are not rounded if digits is Inf. Otherwise, the value of digits rounded to the nearest positive integer is used as the minimum number of significant digits to round numeric values in x to while ensuring their integer parts are not rounded.

If type is "selective", values are individually rounded to more significant digits than digits if that is needed to not round their integer part, such that the number of significant digits might differ between values in a vector or in a column. If type is "expanded", all values in a vector or a in a column are rounded to the same number of significant digits.

See also

round() to round to a specified number of decimal places; signif() to round to a specified number of significant digits; zapsmall() to put small values to zero; formatC() for other ways to format numbers.

Other functions to check equality: are_equal(), check_case(), get_file_path(), not_in(), replace_nonalnum(), replace_vals()

Examples

x1 <- (1e5) / 7
signif(x = x1, digits = 3) # returns 14300
#> [1] 14300
signif_custom(x1, digits = 3) # returns 14286 to not round the integer part
#> [1] 14286

x2 <- 10^c(-1, 5) / 7
signif(x = x2, digits = 3) # returns c(1.43e-02, 1.43e+04)
#> [1] 1.43e-02 1.43e+04
# Increase the number of digits only for the
# second number, returning c(0.0143, 14286.0000)
signif_custom(x2, digits = 3, type = "selective")
#> [1]     0.0143 14286.0000
# Increase the number of digits for both
# numbers, returning c(0.014286, 14286.0000)
signif_custom(x2, digits = 3, type = "expanded")
#> [1] 1.4286e-02 1.4286e+04

x4 <- c(-0.1, 1, -1e4, 1e5) / 7
signif(x = x4, digits = 3)
#> [1] -1.43e-02  1.43e-01 -1.43e+03  1.43e+04
# returns c(-0.0143,   0.143,   -1430,   14300)
signif_custom(x = x4, digits = 3, type = "selective")
#> [1]    -0.0143     0.1430 -1429.0000 14286.0000
# returns c(-0.0143,   0.1430,  -1429.0, 14286)
signif_custom(x = x4, digits = 3, type = "expanded")
#> [1] -1.4286e-02  1.4286e-01 -1.4286e+03  1.4286e+04
# returns c(-0.014286, 0.14286, -1428.6, 14286)

x_df <- data.frame(a = 1:3, b = letters[11:13], c = c(1e5, 1e3, 1) / 7, d = pi)
signif_custom(x_df, type = "selective")
#>   a b         c    d
#> 1 1 k 14286.000 3.14
#> 2 2 l   143.000 3.14
#> 3 3 m     0.143 3.14
signif_custom(x_df, type = "expanded")
#>   a b          c    d
#> 1 1 k 1.4286e+04 3.14
#> 2 2 l 1.4286e+02 3.14
#> 3 3 m 1.4286e-01 3.14