You need to look at the full accuracy of the number representation: > df x cutR 1 0.30800 [0.308,0.309) 2 0.42200 [0.422,0.423) 3 0.17400 [0.173,0.174) 4 0.04709 [0.047,0.048) > Vuew(print(df$x, digits = 20)) Error: could not find function "Vuew" No suitable frames for recover() > View(print(df$x, digits = 20)) [1] 0.307999999999999996003 0.421999999999999986233 0.173999999999999988010 0.047089999999999999913 > print(.308, digits = 20) [1] 0.307999999999999996 > 0.308 == seq(0, 0.310, 0.001)[309] [1] TRUE > print(seq(0, 0.310, 0.001)[309], digits = 20) [1] 0.307999999999999996 >
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Tue, Nov 26, 2013 at 8:46 AM, S Ellison <s.elli...@lgcgroup.com> wrote: > > -----Original Message----- > > > I am attempting to bin a vector of numbers between 0 and 1 into > > > intervals of 0.001 but many values at the endpoints of the intervals > > > are getting binned into the wrong interval. For example, the first 3 > > > rows are binned incorrectly here: > > > > From: Jim Holtman > > FAQ 7.31 > > > Maybe. But > > #and > 0.308 == seq(0, 0.310, 0.001)[309] > # [1] TRUE > > seems to suggest that while some oddities may be explained by finite > precision, 0.308 is exactly represented by the cut sequence here, so .308 > should be OK. > > #in addition, extending the OP's example > df <- data.frame(x=c(0.308,0.422,0.174,0.04709)) > df$bucket <- cut2(df$x,seq(0,1,0.001),oneval=FALSE) > df$cutR <- cut(df$x,seq(0,1,0.001),right=FALSE) > df > > # x bucket cutR > # 1 0.30800 [0.307,0.308) [0.308,0.309) > # 2 0.42200 [0.421,0.422) [0.422,0.423) > # 3 0.17400 [0.173,0.174) [0.173,0.174) > # 4 0.04709 [0.047,0.048) [0.047,0.048) > > implies that cut2 is not doing the same thing as cut despite the same > intended outcome (at least on R 3.0.1, my present version at work). > > This may be one for Frank Harrell ... > > S Ellison > > > > ******************************************************************* > This email and any attachments are confidential. Any u...{{dropped:13}} ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.