我認爲這應該等同於Rolands解決方案。
x <- c(4, 4.0, 4.00, 28.382, 120,
82.3, 100, 100.0, 30.0003)
x
ifelse(x == signif(x, 1), NA, x)
ifelse(x == signif(x, 2), NA, x)
ifelse(x == signif(x, 3), NA, x)
在任何情況下,至少有給人顯著位數不正確的號碼,如「4.00」和「100.0」的情況下同樣的問題。
如上所述,解決方案的一部分是將數字視爲字符串。僅僅將數字轉換爲字符是不夠的,它們必須被讀入,這需要小心。 read.table
功能組can come in handy中的colClasses
參數。
xc <- c("4", "4.0", "4.00", "28.382", "120",
"82.3", "100", "100.0", "30.0003")
xc
# "4" "4.0" "4.00" "28.382" "120" "82.3" "100" "100.0" "30.0003"
ifelse(xc == signif(as.numeric(xc), 1), NA, xc)
# "NA" "4.0" "4.00" "28.382" "120" "82.3" "NA" "100.0" "30.0003"
只刪除「4」和「100」。這看起來很有希望,但如果我們進一步發展,我們會發現並非所有事情都應該如此。
ifelse(xc == signif(as.numeric(xc), 2), NA, xc)
# "NA" "4.0" "4.00" "28.382" "120" "82.3" "NA" "100.0" "30.0003"
ifelse(xc == signif(as.numeric(xc), 3), NA, xc)
# "NA" "4.0" "4.00" "28.382" "120" "82.3" "NA" "100.0" "30.0003"
理由可以證明這樣
2 == "2"
# TRUE – only what's between the quotes is compared
2.0 == "2"; 02 == "2"
# TRUE
# TRUE – R removes what's considered numerically empty characters
2 == "2.0"
# FALSE – strings aren't modified.
2 == as.numeric("2.0")
# TRUE – that is, unless you explicitly request it.
這也是值得銘記的字符串的比較是基於字母順序,即使字符串容易可以解釋爲數字。
2 < "2.0"
# TRUE
2 > "2.0"
# FALSE
"2.0" < "2.00"
# TRUE
sort(xc)
# "100" "100.0" "120" "28.382" "30.0003" "4" "4.0" "4.00" "82.3"
到目前爲止,我發現這個問題的唯一完整的解決方法是一個小黑客。它包括分離出包含小數點分隔符(「。」)的字符串,並用「1」(或任何非零數字)替換這些字符串的最後一個字符。因此將「4.0」變成「4.1」,但保持「100」原樣。這個新的矢量然後被用作比較的基礎。
xc.1 <- xc
decimal <- grep(".", xc, fixed=TRUE)
xc.1[decimal] <- gsub(".$", "1", xc[decimal])
xc.1 <- as.numeric(xc.1)
xc
# "4" "4.0" "4.00" "28.382" "120" "82.3" "100" "100.0" "30.0003"
ifelse(xc.1 == signif(xc.1, 1), NA, xc)
# "NA" "4.0" "4.00" "28.382" "120" "82.3" "NA" "100.0" "30.0003"
ifelse(xc.1 == signif(xc.1, 2), NA, xc)
# "NA" "NA" "4.00" "28.382" "NA" "82.3" "NA" "100.0" "30.0003"
ifelse(xc.1 == signif(xc.1, 3), NA, xc)
# "NA" "NA" "NA" "28.382" "NA" "NA" "NA" "100.0" "30.0003"
如果你想實際計算有效數字的數量,可以用一個小循環來完成。
n <- 7
# true counts
xc.count <- vector(length=length(xc.1))
for (i in n:1) xc.count[xc.1 == signif(xc.1, i)] <- i
xc.count
# 1 2 3 5 2 3 1 4 6
# simple counts
x.count <- vector(length=length(x))
for (i in n:1) x.count[x == signif(x, i)] <- i
x.count
# 1 1 1 5 2 3 1 1 6
如果您將存儲的浮動物的打印表示與實際存儲的值混淆,則可能會匆匆忙忙。雖然Roland的解決方案看起來不錯,但我強烈建議您將實際報告的精度轉換爲字符串並從那裏開始工作。 –