Hi, I am using 32-bit R v 2.15.1, on Mac OsX v 10.6.8 with R GUI 1.52 Leopard build 32-bit (6188). I have also replicated this error on R 2.13.2 on a windows 7 machine. In some sequences of numbers, I am unable to use the which() function to index certain values. It seems to be primarily numbers with a 3 in the decimal. For example:> S=seq(0,4,0.01)> which(S==2.02)[1] 203> which(S==2.03)integer(0)> which(S==2.04)[1] 205> which(S==2.04)[1] 205> which(S==3.03)integer(0)> which(S==3.3)integer(0)> which(S==3.)[1] 301> which(S==3.5)[1] 351>I'm not even sure if this is a bug, or just some misunderstanding of the seq() or which() functions, but any thoughts would be appreciated. TB [[alternative HTML version deleted]]
It looks like an example of FAQ 7.31 Why doesn't R think these numbers are equal? John Kane Kingston ON Canada> -----Original Message----- > From: tomasbird at gmail.com > Sent: Wed, 15 Aug 2012 22:37:17 +1000 > To: r-help at r-project.org > Subject: [R] which() function not finding certain numbers > > Hi, > > I am using 32-bit R v 2.15.1, on Mac OsX v 10.6.8 with R GUI 1.52 Leopard > build 32-bit (6188). I have also replicated this error on R 2.13.2 on a > windows 7 machine. > > In some sequences of numbers, I am unable to use the which() function to > index certain values. It seems to be primarily numbers with a 3 in the > decimal. > For example: > >> S=seq(0,4,0.01) > >> which(S==2.02) > > [1] 203 > >> which(S==2.03) > > integer(0) > >> which(S==2.04) > > [1] 205 > >> which(S==2.04) > > [1] 205 > >> which(S==3.03) > > integer(0) > >> which(S==3.3) > > integer(0) > >> which(S==3.) > > [1] 301 > >> which(S==3.5) > > [1] 351 > >> > > I'm not even sure if this is a bug, or just some misunderstanding of the > seq() or which() functions, but any thoughts would be appreciated. > > TB > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
FAQ 7.31 ?is.equal --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Tom Bird <tomasbird at gmail.com> wrote:>Hi, > >I am using 32-bit R v 2.15.1, on Mac OsX v 10.6.8 with R GUI 1.52 >Leopard >build 32-bit (6188). I have also replicated this error on R 2.13.2 on >a >windows 7 machine. > >In some sequences of numbers, I am unable to use the which() function >to >index certain values. It seems to be primarily numbers with a 3 in >the >decimal. >For example: > >> S=seq(0,4,0.01) > >> which(S==2.02) > >[1] 203 > >> which(S==2.03) > >integer(0) > >> which(S==2.04) > >[1] 205 > >> which(S==2.04) > >[1] 205 > >> which(S==3.03) > >integer(0) > >> which(S==3.3) > >integer(0) > >> which(S==3.) > >[1] 301 > >> which(S==3.5) > >[1] 351 > >> > >I'm not even sure if this is a bug, or just some misunderstanding of >the >seq() or which() functions, but any thoughts would be appreciated. > >TB > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at 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.
On Wed, Aug 15, 2012 at 10:37:17PM +1000, Tom Bird wrote:> Hi, > > I am using 32-bit R v 2.15.1, on Mac OsX v 10.6.8 with R GUI 1.52 Leopard > build 32-bit (6188). I have also replicated this error on R 2.13.2 on a > windows 7 machine. > > In some sequences of numbers, I am unable to use the which() function to > index certain values. It seems to be primarily numbers with a 3 in the > decimal. > For example: > > > S=seq(0,4,0.01) > > > which(S==2.02) > > [1] 203 > > > which(S==2.03) > > integer(0) > > > which(S==2.04) > > [1] 205 > > > which(S==2.04) > > [1] 205 > > > which(S==3.03) > > integer(0) > > > which(S==3.3) > > integer(0) > > > which(S==3.) > > [1] 301 > > > which(S==3.5) > > [1] 351Hi. This is a rounding problem. print(S[204], digits=20) [1] 2.0300000000000002487 print(2.03, digits=20) [1] 2.0299999999999998046 Use function round(, digits), for example S <- round(seq(0,4,0.01), digits=7) which(S==2.03) [1] 204 which(S==3.03) [1] 304 Hope this helps. Petr Savicky.
HI, Try this: ?S<-as.numeric(formatC(seq(0,4,0.01),digits=3)) ?which(S==2.02) #[1] 203 ?which(S==2.03) #[1] 204 ?which(S==3.03) #[1] 304 ?which(S==3.3) #[1] 331 ?which(S==3.) #[1] 301 A.K. ----- Original Message ----- From: Tom Bird <tomasbird at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, August 15, 2012 8:37 AM Subject: [R] which() function not finding certain numbers Hi, I am using 32-bit R v 2.15.1, on Mac OsX v 10.6.8 with R GUI 1.52 Leopard build 32-bit (6188).? I have also replicated this error on R 2.13.2 on a windows 7 machine. In some sequences of numbers,? I am unable to use the which() function to index certain values.? ? It seems to be primarily numbers with a 3 in the decimal. For example:> S=seq(0,4,0.01)> which(S==2.02)[1] 203> which(S==2.03)integer(0)> which(S==2.04)[1] 205> which(S==2.04)[1] 205> which(S==3.03)integer(0)> which(S==3.3)integer(0)> which(S==3.)[1] 301> which(S==3.5)[1] 351>I'm not even sure if this is a bug,? or just some misunderstanding of the seq() or which() functions,? but any thoughts would be appreciated. TB ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.