For numbers ending in 5 Excel always rounds up and I want to reproduce this in R rather than use the round function. I have tried: x<-as.integer(x*100+0.5)/100 and x<-floor(x*100+0.5)/100 However, some values of x cause problems e.g x<-floor(4.145*100+0.5)/100 4.14 I have tried breaking it down into steps and force the results to 10 significant figures in the following function, which seems to work roundoff<-function(x){ a<-signif(x*100+0.5,digits=10) roundoff<-floor(a)/100 roundoff } Is there a more sensible way of do this for all numbers? Thanks Mike [[alternate HTML version deleted]]
Does this script do what you want? cround <- function(x,digits=0) { a <- ifelse(x>0,.5,-.5) if (digits==0) { floor(x+a) } else { m <- 10^digits floor(x*m+a)/m } } > cround(1.4535,1) [1] 1.5 > cround(1.4535,2) [1] 1.45 > cround(1.4535,3) [1] 1.454 > cround(1.4535,4) [1] 1.4535 - Hedderik.
I don't have the reference, but a biologist friend of mine once showed me a refereed journal article that purported to demonstrate numerical errors made by MSExcel. This would have been Excel97 or Excel2000... In any case, the journal's scope was biological in nature and the article was of interest since Excel is heavily used in that community. -david paul -----Original Message----- From: Duncan Murdoch [mailto:dmurdoch at pair.com] Sent: Wednesday, June 04, 2003 8:34 AM To: MSchwartz at medanalytics.com Cc: R-help at stat.math.ethz.ch Subject: Re: [R] Rounding problem R vs Excel On 04 Jun 2003 00:24:08 -0500, you wrote:>Excel 2002 (XP): > >Cell Formula Value >= 0.5 - 0.4 - 0.1 0.00000000000000000000E+00 >=(0.5 - 0.4 - 0.1) -2.77555756156289000000E-17...>What is interesting is the change in the displayed value in Excel when >the second formula is surrounded by parens (which I found purely by >accident). This would suggest that there may be something going on in >the parsing of the cell formula that affects the calculation and >displayed value."Interesting"? I'd say "horrifying". When (expr) does not evaluate the same as expr, what can you trust? Duncan Murdoch ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help