zapsmall

zapsmall(x, digits = getOption(“digits”)))
If some values of x are nearly zero relative to the larger values in x, then zapsmall will set those small values to zero.
  • x – A numeric vector.
  • digits – An integer indicating the precision.
While not a perfect rule, anticipate values of x that are, in absolute value, less than 1 and are 10^digits smaller than the largest values of x to be set to zero.

Example. Below we look at the ratio of the largest and smallest values in a couple of vectors and the threshold of digits where the smallest value gets set to zero.
> 
> x1 <- c(52938, 1928, 12, 2, 0.0053)
> x2 <- c(52938, 1928, 12, 2, 0.005)
> y  <- c(52938, 1928, 12, 2, 0)
> 
> log(max(x1) / min(x1), 10)
[1] 6.999492
> all(zapsmall(x1, 6) - y == 0)
[1] TRUE
> all(zapsmall(x1, 7) - y == 0)
[1] FALSE
> 
> log(max(x2) / min(x2), 10)
[1] 7.024798
> all(zapsmall(x2, 7) - y == 0)
[1] TRUE
> all(zapsmall(x2, 8 ) - y == 0)
[1] FALSE
> 
The all function was also used in the code above.

Leave a Reply