當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R zapsmall 數字四舍五入:將小數變為零

R語言 zapsmall 位於 base 包(package)。

說明

zapsmall 確定用於調用 round(x, digits = dr)digits 參數 dr,使得接近於零的值(與最大絕對值相比)為 ‘zapped’,即替換為 0

用法

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

參數

x

數字或複數向量或任何Rnumber-like 對象有一個round方法和基本算術方法包括log10().

digits

指示要使用的精度的整數。

mFUN

數值(或複數)xfunction(x, ina)logical ina := is.na(x) 返回最大 abs(x) 值的大小順序的正數。默認值是向後兼容的,但不穩健,例如,當 x 具有無限條目時不是很有用。

min.d

一個整數,指定 mFUN(*) > 0 時在生成的 round(x, digits=*) 調用中使用的最小位數。

例子

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))

參考

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

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Rounding of Numbers: Zapping Small Ones to Zero。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。