Learn R Programming

gtsummary (version 1.2.1)

style_sigfig: Implement significant figure-like rounding

Description

Converts a numeric argument into a string that has been rounded to a significant figure-like number. Scientific notation output is avoided, however, and additional significant figures may be displayed for large numbers. For example, if the number of significant digits requested is 2, 123 will be displayed (rather than 120 or 1.2x10^2).

Usage

style_sigfig(x, digits = 2)

Arguments

x

Numeric vector

digits

Integer specifying the minimum number of significant digits to display

Value

A character vector of styled numbers

Details

If 2 sig figs are input, the number is rounded to 2 decimal places when abs(x) < 1, 1 decimal place when abs(x) >= 1 & abs(x) < 10, and to the nearest integer when abs(x) >= 10.

Examples

Run this code
# NOT RUN {
c(0.123, 0.9, 1.1234, 12.345, -0.123, -0.9, -1.1234, -12.345, NA, -0.001) %>%
  style_sigfig()
# }

Run the code above in your browser using DataLab