(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