baixiwei
baixiwei

Reputation: 1029

Why doesn't cut work as expected in R?

Why don't these two return the same result?

    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)

Upvotes: 1

Views: 264

Answers (2)

Fabio Marroni
Fabio Marroni

Reputation: 453

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)

Upvotes: 1

James
James

Reputation: 66834

Representation error. Floating point approximation of numbers is only exact if the number is a combination of certain powers of 2. Other numbers are mapped to these numbers. Different algorithms to produce a number may do so in different ways and have different errors associated with them (ie above or below the expected value). In this case:

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

Upvotes: 5

Related Questions