(tmp1 <- seq(0, 0.2, 0.01)[16])
# [1] 0.15
(tmp2 <- seq(0, 0.2, 0.05)[4])
# [1] 0.15
和
identical(tmp1, tmp2)
# [1] FALSE
all.equal(tmp1, tmp2) # test for 'near' equality
[1] TRUE
的根本原因是floating point precision做。然而,這會導致嘗試使用match
識別序列中的子序列的時候出了問題,例如:
match(seq(0, 0.2, 0.05), seq(0, 0.2, 0.01))
# [1] 1 6 11 NA 21
是否有match
的替代方案,是爲all.equal
的identical
模擬?
也許用'round'? 'match(round(tmp2,2),round(tmp1,2))' –
這是個好主意,所以我只需要巧妙地選擇數字參數。 – Alex
@Alex - 'round'絕對適用於矢量。否則就沒用了。 – thelatemail