Dear list, I can not understand why the expression in the subject does not work correct: > dcrn[which(fn == inve[2])] numeric(0) > inve[2] [1] 406.7 > dcrn[which(fn == 406.7)] [1] 1.3994e-07 1.3988e-07 1.3953e-07 1.3966e-07 1.3953e-07 1.3968e-07 Is this a kick self problem or an bug? Thaks very much Thomas
On Wednesday 08 October 2003 11:27, Thomas Bock wrote:> Dear list, > > I can not understand why the expression in > > the subject does not work correct: > > dcrn[which(fn == inve[2])] > > numeric(0) > > > inve[2] > > [1] 406.7 > > > dcrn[which(fn == 406.7)] > > [1] 1.3994e-07 1.3988e-07 1.3953e-07 1.3966e-07 1.3953e-07 > 1.3968e-07The reason is that you shouldn't compare numeric output like that, consider the following example: R> x <- 406.7 + 1e-5 R> x [1] 406.7 R> x == 406.7 [1] FALSE whereas R> x <- 406.7 + 1e-20 R> x [1] 406.7 R> x == 406.7 [1] TRUE that is 1.) `==' comparisons have a certain tolerance 2.) the print output is not necessarily "precisely" your number Instead of using `==' you should use a comparison with a certain tolerance you can specify... hth, Z> Is this a kick self problem or an bug? > > Thaks very much > Thomas > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Thomas Bock <Thomas.Bock at ptb.de> writes:> Dear list, > > I can not understand why the expression in > the subject does not work correct: > > > dcrn[which(fn == inve[2])] > numeric(0) > > inve[2] > [1] 406.7 > > dcrn[which(fn == 406.7)] > [1] 1.3994e-07 1.3988e-07 1.3953e-07 1.3966e-07 1.3953e-07 1.3968e-07 > > Is this a kick self problem or an bug?Kick self, most likely. What is inve[2] - 406.7 -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Your question has been answered by Achim and Peter Dalgaard (at least). Just a note: Using a[which(logic)] looks like a clumsy and inefficient way of writing a[ logic ] and I think you shouldn't propagate its use ... Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <><
At Wednesday 03:06 PM 10/8/2003 +0200, Martin Maechler wrote:>Your question has been answered by Achim and Peter Dalgaard (at least). > >Just a note: > >Using > a[which(logic)] >looks like a clumsy and inefficient way of writing > a[ logic ] > >and I think you shouldn't propagate its use ...What then is the recommended way of treating an NA in the logical subset as a FALSE? (Or were you just talking about the given example, which didn't have this issue. However, you admonition seemed more general.) As in: > x <- 1:4 > y <- c(1,2,NA,4) > x[y %% 2 == 0] [1] 2 NA 4 > x[which(y %% 2 == 0)] [1] 2 4 > Sometimes one might want the first result, but more usually, I want the second, and using which() seems a convenient way to get it. -- Tony Plate
Here are some different ways of doing this. Don't know whether any could be considered superior to the others. # y[x==5] regarding NAs in x as not matching x <- c(5, NA, 7, 5, NA, 3) y <- c(1, 2, 3, 4, 5, 6) subset(y,x==5) y[x %in% 5] y[x %in% c(5)] y[which(x==5)] --- Date: Wed, 08 Oct 2003 08:58:32 -0600 From: Tony Plate <tplate at blackmesacapital.com> At Wednesday 03:06 PM 10/8/2003 +0200, Martin Maechler wrote:>Your question has been answered by Achim and Peter Dalgaard (at least). > >Just a note: > >Using > a[which(logic)] >looks like a clumsy and inefficient way of writing > a[ logic ] > >and I think you shouldn't propagate its use ...What then is the recommended way of treating an NA in the logical subset as a FALSE? (Or were you just talking about the given example, which didn't have this issue. However, you admonition seemed more general.) As in:> x <- 1:4 > y <- c(1,2,NA,4) > x[y %% 2 == 0][1] 2 NA 4> x[which(y %% 2 == 0)][1] 2 4>Sometimes one might want the first result, but more usually, I want the second, and using which() seems a convenient way to get it. -- Tony Plate _______________________________________________ No banners. No pop-ups. No kidding. Introducing My Way - http://www.myway.com
Achim Zeileis <zeileis at ci.tuwien.ac.at> wrote: R> x <- 406.7 + 1e-20 R> x [1] 406.7 R> x == 406.7 [1] TRUE that is 1.) `==' comparisons have a certain tolerance No, all.equal() supports tolerance, == does not. Consider > .Machine$double.eps [1] 2.220446e-16 That is, the smallest relative difference that can be represented on my (fairly typical IEEE-conforming) machine is 2 in 10 to the 16th. 1e-20/406.7 is 2.458815e-23, which is a factor of 10 million too small a relative difference for the hardware to be able to represent. So in x <- 406.7 + 1e-20 the value of x is identical to 406.7 in every bit. Instead of using `==' you should use a comparison with a certain tolerance you can specify... Such as ?all.equal