Hi, I'm having trouble with the "which" or the "seq" function, I'm not sure. Here's an example : > lat=seq(1,2,by=0.1) > lat [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 > which(lat==1) [1] 1 > which(lat==1.1) [1] 2 > which(lat==1.2) [1] 3 > which(lat==1.3) [1] 4 > which(lat==1.4) [1] 5 > which(lat==1.5) [1] 6 > which(lat==1.6) [1] 7 > which(lat==1.7) *integer(0)* > which(lat==1.8) [1] 9 > which(lat==1.9) [1] 10 > which(lat==2) [1] 11 This doesn't seem to happen with integers. Am I missing something ?? Is there a better function for non-integers ? Thanks a lot, Melanie
Melanie Abecassis <Melanie.Abecassis at noaa.gov> wrote in news:481002B0.3080704 at noaa.gov:> Hi, > I'm having trouble with the "which" or the "seq" function, I'm not > sure. Here's an example : > > > > lat=seq(1,2,by=0.1) > > lat > [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 > > > which(lat==1) > [1] 1 > > which(lat==1.1)snip> [1] 7 > > which(lat==1.7) > *integer(0)* > This doesn't seem to happen with integers. > Am I missing something ?? Is there a better function for > non-integers ?Read the FAQ (Section 7.31 Why doesn't R think these numbers are equal? http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f and look at this:> lat - 1.7[1] -7.000000e-01 -6.000000e-01 -5.000000e-01 -4.000000e-01 [5] -3.000000e-01 -2.000000e-01 -1.000000e-01 2.220446e-16 [9] 1.000000e-01 2.000000e-01 3.000000e-01 -- David Winsemius
G'day Melanie, On Wed, 23 Apr 2008 17:46:56 -1000 Melanie Abecassis <Melanie.Abecassis at noaa.gov> wrote:> This doesn't seem to happen with integers. > Am I missing something ??Yes, FAQ 7.31: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f> Is there a better function for non-integers ?> which(sapply(tt, function(x) isTRUE(all.equal(x,1.7))))[1] 8 seems to work. HTH. Cheers, Berwin =========================== Full address ============================Berwin A Turlach Tel.: +65 6515 4416 (secr) Dept of Statistics and Applied Probability +65 6515 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: statba at nus.edu.sg Singapore 117546 http://www.stat.nus.edu.sg/~statba