Hello, I found that round() does not behave as I expected. Have you had similar experience as following?> x<-seq(0.5,10.5,by=1) > x[1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5> round(x)[1] 0 2 2 4 4 6 6 8 8 10 10> cbind(x,round(x))x [1,] 0.5 0 [2,] 1.5 2 [3,] 2.5 2 [4,] 3.5 4 [5,] 4.5 4 [6,] 5.5 6 [7,] 6.5 6 [8,] 7.5 8 [9,] 8.5 8 [10,] 9.5 10 [11,] 10.5 10 Is this a well-known bug? Thanks in advance, Dongseok Choi, Ph.D. Assistant Professor Division of Biostatistics Department of Public Health & Preventive Medicine Oregon Health & Science University 3181 SW Sam Jackson Park Road, CB-669 Portland, OR 97239-3098 TEL) 503-494-5336 FAX) 503-494-4981 choid@ohsu.edu [[alternative HTML version deleted]]
This in NOT a bug. It is a convention (the IEEE standard). Read the help on round(). I.e. RTFM. cheers, Rolf Turner rolf at math.unb.ca
?round 'round' rounds the values in its first argument to the specified number of decimal places (default 0). Note that for rounding off a 5, the IEEE standard is used, "_go to the even digit_". Therefore 'round(0.5)' is '0' and 'round(-1.5)' is '-2'. Dongseok Choi wrote:> Hello, > > I found that round() does not behave as I expected. > Have you had similar experience as following? > > >>x<-seq(0.5,10.5,by=1) >>x > > [1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 > >>round(x) > > [1] 0 2 2 4 4 6 6 8 8 10 10 > >>cbind(x,round(x)) > > x > [1,] 0.5 0 > [2,] 1.5 2 > [3,] 2.5 2 > [4,] 3.5 4 > [5,] 4.5 4 > [6,] 5.5 6 > [7,] 6.5 6 > [8,] 7.5 8 > [9,] 8.5 8 > [10,] 9.5 10 > [11,] 10.5 10 > > > Is this a well-known bug? > > Thanks in advance, > > > Dongseok Choi, Ph.D. > Assistant Professor > Division of Biostatistics > Department of Public Health & Preventive Medicine > Oregon Health & Science University > 3181 SW Sam Jackson Park Road, CB-669 > Portland, OR 97239-3098 > TEL) 503-494-5336 > FAX) 503-494-4981 > choid at ohsu.edu > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
Dongseok Choi wrote:> Hello, > > I found that round() does not behave as I expected. > Have you had similar experience as following?You have found the DOCUMENTED "round to even" rule, hence not a bug... Uwe Ligges> >>x<-seq(0.5,10.5,by=1) >>x > > [1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 > >>round(x) > > [1] 0 2 2 4 4 6 6 8 8 10 10 > >>cbind(x,round(x)) > > x > [1,] 0.5 0 > [2,] 1.5 2 > [3,] 2.5 2 > [4,] 3.5 4 > [5,] 4.5 4 > [6,] 5.5 6 > [7,] 6.5 6 > [8,] 7.5 8 > [9,] 8.5 8 > [10,] 9.5 10 > [11,] 10.5 10 > > > Is this a well-known bug? > > Thanks in advance, > > > Dongseok Choi, Ph.D. > Assistant Professor > Division of Biostatistics > Department of Public Health & Preventive Medicine > Oregon Health & Science University > 3181 SW Sam Jackson Park Road, CB-669 > Portland, OR 97239-3098 > TEL) 503-494-5336 > FAX) 503-494-4981 > choid at ohsu.edu > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Tue, 22 Feb 2005 10:10:58 -0800, "Dongseok Choi" <choid at ohsu.edu> wrote :>Hello, > > I found that round() does not behave as I expected. > Have you had similar experience as following? > >> x<-seq(0.5,10.5,by=1) >> x > [1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 >> round(x) > [1] 0 2 2 4 4 6 6 8 8 10 10 >> cbind(x,round(x)) > x > [1,] 0.5 0 > [2,] 1.5 2 > [3,] 2.5 2 > [4,] 3.5 4 > [5,] 4.5 4 > [6,] 5.5 6 > [7,] 6.5 6 > [8,] 7.5 8 > [9,] 8.5 8 >[10,] 9.5 10 >[11,] 10.5 10 > > > Is this a well-known bug?As others have said, it's not a bug at all. If you would like to round up instead, what you should do is use a function like this:> roundup <- function(x) trunc(x+0.5)> x <- -5:5 + 0.5 > x[1] -4.5 -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3.5 4.5 5.5> round(x)[1] -4 -4 -2 -2 0 0 2 2 4 4 6> roundup(x)[1] -4 -3 -2 -1 0 1 2 3 4 5 6 If this doesn't handle negatives the way you want, play around a bit with abs(). Duncan Murdoch
Reasonably Related Threads
- How to increase memory for R on Soliars 10 with 16GB and 64bit R
- error when compiling "stats" library in R-2.3.1 on Solaris x86
- rgl install problem on Solaris 10 X86
- Compiling Matrix on Solaris 10 x86-64 Sun Studio 12
- questions on local customized R distribution CD