| count_sigfigs | R Documentation |
Count number of significant digits (figures), option to include trailing zeros
count_sigfigs(x, digits = getOption("digits"), countTrailing = FALSE)
x |
numeric or character vector; only character input is allowed if |
digits |
integer of length 1. Optional. Default uses value from |
countTrailing |
logical. Should trailing zeros be counted? Defaults to |
Count number of significant digits, either from a (measurement) precision or a (mathematical) numeric
perspective via the countTrailing option. When set to TRUE and provided a character input
x, this option will count trailing zeros as significant digits. Leading zeros are (almost) never counted; the
single exception is when countTrailing=TRUE and x contains values that would be coerced to 0
via e.g. as.integer or as.numeric; see Notes.
By default, countTrailing=FALSE; this counts significant digits from a (mathematical) numeric perspective.
Setting to TRUE counts significant digits from a (measurement) precision perspective.
Because R (and most other analysis tools/languages) will automatically strip trailing zeros for
integer or numeric data types, this function will throw an error if countTrailing=TRUE and
class(x) != "character".
This function does not perform any explicit rounding or truncation of input within reasonable limits; if input precision exceeds system limit, standard R rounding rules will apply, silently.
An integer vector of significant digit counts equal in length to input vector.
Zero is a special case. If countTrailing=FALSE, will return zero. If countTrailing=TRUE, all
digits are counted, excluding digits following the exponent notation (e or E) if applicable. See examples
This function assumes that if you provide character inputs that would evaluate to 0 if treated as a number, this is
in fact meaningful from a precision/measurement perspective.
set.seed(1)
x <- mapply(signif, rnorm(10), 1:10)
count_sigfigs(x)
count_sigfigs(x, digits = 10)
#Also works for scientific notation
count_sigfigs(c(1E10, -1E10, -000001E-10)) #all 1
count_sigfigs(c("1.0001", 1.0001, 0001.0001)) #all 5
#digits option
#By default, will use local number of digits, which is 7
getOption("digits") #default should be 7
count_sigfigs(0.1234567) #7
count_sigfigs(0.12345678) #8
count_sigfigs(0.123456789) #8
count_sigfigs(0.123456789, digits = 8) #9
#countTrailing
count_sigfigs("1.0100", countTrailing = TRUE)
count_sigfigs("1.0100E10", countTrailing = TRUE)
#zero handling
zeros_decimal <- c("0", "0.0", "00.0", "00.00", "0.000")
zeros_exponent <- c("0E0", "0.0E0", "00.0E0", "00.00E0", "0.000E0")
count_sigfigs(zeros_decimal) #all zero
count_sigfigs(zeros_decimal, countTrailing = TRUE) #1,2,3,4,4
count_sigfigs(zeros_exponent) #all zero
count_sigfigs(zeros_exponent, countTrailing = TRUE) #also 1,2,3,4,4
## Not run:
#compare:
count_sigfigs(123.12345679, digits = 7) #need higher digits option
count_sigfigs(123.12345679, digits = 15) #what you expected
count_sigfigs(123.12345679, digits = 16) #probably not what you expected
#also
count_sigfigs(1000000001.12345, digits = 15) #pushing it
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.