[R] one is not one

Roger Bivand Roger.Bivand at nhh.no
Sat Oct 21 11:18:48 CEST 2006


On Sat, 21 Oct 2006, Patrick Giraudoux wrote:

> Folks,
> 
> I have got a strange behaviour when testing this:
> 
> sum(x) != 1

FAQ 7.31 Why doesn't R think these numbers are equal?:

print(sum(x), digits=20)
identical(sum(x), 1)
all.equal(sum(x), 1)


> 
> let us set
> 
> x<-c(70,134,1,5,0)
> 
> and transform it in a vector of probabilities
> 
> x<-x/sum(x)
> 
> One expect  sum(x) should be equal to 1, which is apparently the case
> 
> > sum(x)
> [1] 1
> 
> However, when I try to test it I get:
> 
> > if(sum(x) !=1) print("lost") else ("OK")
> [1] "lost"
> 
> Which means that actually sum(x) is NOT considered equal to 1...
> 
> Any idea about what is going wrong?
> 
> Patrick
> 
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-help mailing list