为什么不在R中按预期削减工作?

时间:2013-07-12 14:35:59

标签: r

为什么这两个不会返回相同的结果?

    D = data.frame( x=c( 0.6 ) )

    D$binned = cut( D$x, seq( 0.50,0.70,0.025 ), include.lowest=TRUE, right=FALSE )
    D # 0.6 is binned correctly as [0.6,0.625)

    D$binned = cut( D$x, seq( 0.55,0.65,0.025 ), include.lowest=TRUE, right=FALSE )
    D # 0.6 is binned incorrectly as [0.575,0.6)

2 个答案:

答案 0 :(得分:5)

表示错误。如果数字是某些2的幂的组合,则数字的浮点近似仅是精确的。其他数字映射到这些数字。产生数字的不同算法可以以不同方式这样做并且具有与它们相关联的不同误差(即,高于或低于预期值)。在这种情况下:

print(D$x,digits=22)
[1] 0.5999999999999999777955
print(seq(0.5,0.7,0.025)[5],digits=22)
[1] 0.5999999999999999777955
> print(seq(0.55,0.65,0.025)[3],digits=22)
[1] 0.6000000000000000888178

答案 1 :(得分:1)

D$binned = cut( D$x, round(seq( 0.55,0.65,0.025 ),3), include.lowest=TRUE, right=FALSE )

D

x binned

1 0.6 [0.6,0.625)