zapsmall {base}R Documentation

Rounding of Numbers: Zapping Small Ones to Zero

Description

zapsmall determines a digits argument dr for calling round(x, digits = dr) such that values close to zero (compared with the maximal absolute value) are ‘zapped’, i.e., replaced by 0.

Usage

zapsmall(x, digits = getOption("digits"),
         mFUN = function(x, ina) max(abs(x[!ina])),
         min.d = 0L)

Arguments

x

a numeric or complex vector or any R number-like object which has a round method and basic arithmetic methods including log10().

digits

integer indicating the precision to be used.

mFUN

a function(x, ina) of the numeric (or complex) x and the logical ina := is.na(x) returning a positive number in the order of magnitude of the maximal abs(x) value. The default is back compatible but not robust, and e.g., not very useful when x has infinite entries.

min.d

an integer specifying the minimal number of digits to use in the resulting round(x, digits=*) call when mFUN(*) > 0.

References

Chambers, J. M. (1998) Programming with Data. A Guide to the S Language. Springer.

Examples

x2 <- pi * 100^(-1:3)
   print(  x2 / 1000, digits = 4)
zapsmall(  x2 / 1000, digits = 4)
zapsmall(  x2 / 1000) # automatical digits
zapsmall(c(x2 / 1000, Inf)) # round()s to integer ..
zapsmall(c(x2 / 1000, Inf), min.d=-Inf) # everything  is small wrt  Inf

## using a *robust* mFUN
mF_rob <- function(x, ina) boxplot.stats(x, do.conf=FALSE)$stats[5]







## with robust mFUN(), 'Inf' is no longer distorting the picture:
zapsmall(c(x2 / 1000, Inf), mFUN = mF_rob)
zapsmall(c(x2 / 1000, 999), mFUN = mF_rob)
zapsmall(c(x2 / 1000, Inf), mFUN = mF_rob, min.d = -5)
zapsmall(c(x2 / 1000, 999), mFUN = mF_rob, min.d = -5)

zapsmall(exp(1i*0:4*pi/2))

[Package base version 4.3.0 Index]