Heemun Kwok
2011-Jul-27 03:50 UTC
[R] "match" and "which" give NA for some values, but not others
Hello list, I am having trouble with "match" and "which" giving a NA result for some values, but not others. Here is a simple example:> aaa <- seq(0,1,by=0.05)> aaa[1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 [16] 0.75 0.80 0.85 0.90 0.95 1.00> match(0.5,aaa)[1] 11> match(0.6,aaa)[1] NA> which(aaa==0.5)[1] 11> which(aaa==0.6)integer(0)> sessionInfo()R version 2.12.2 (2011-02-25) Platform: i386-pc-mingw32/i386 (32-bit) Am I missing something? Any thoughts? Thank you, Heemun ------------------------------------------- Heemun Kwok, MD, MS Acting Instructor, Emergency Medicine University of Washington School of Medicine Box 359702 325 Ninth Avenue Seattle, WA 98104 Tel: 206-744-8461 Fax: 206-744-4097 Email: Heemun at uw.edu
Daniel Nordlund
2011-Jul-27 07:08 UTC
[R] "match" and "which" give NA for some values, but not others
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Heemun Kwok > Sent: Tuesday, July 26, 2011 8:50 PM > To: r-help at r-project.org > Subject: [R] "match" and "which" give NA for some values, but not others > > Hello list, > I am having trouble with "match" and "which" giving a NA result for some > values, but not others. Here is a simple example: > > > aaa <- seq(0,1,by=0.05) > > > aaa > [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 > 0.70 > [16] 0.75 0.80 0.85 0.90 0.95 1.00 > > > match(0.5,aaa) > [1] 11 > > > match(0.6,aaa) > [1] NA > > > which(aaa==0.5) > [1] 11 > > > which(aaa==0.6) > integer(0) > > > sessionInfo() > R version 2.12.2 (2011-02-25) > Platform: i386-pc-mingw32/i386 (32-bit) > > Am I missing something? Any thoughts? > > Thank you, > Heemun > >Take a look at R FAQ 7.31. Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA
Rolf Turner
2011-Jul-27 07:42 UTC
[R] "match" and "which" give NA for some values, but not others
You are failing to take account of floating point arithmetic. This is FAQ 7.31. Really, it is advisable to check the FAQ before firing off a naive inquiry to the r-help list. cheers, Rolf Turner On 27/07/11 15:50, Heemun Kwok wrote:> Hello list, > I am having trouble with "match" and "which" giving a NA result for some values, but not others. Here is a simple example: > >> aaa<- seq(0,1,by=0.05) >> aaa > [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 > [16] 0.75 0.80 0.85 0.90 0.95 1.00 > >> match(0.5,aaa) > [1] 11 > >> match(0.6,aaa) > [1] NA > >> which(aaa==0.5) > [1] 11 > >> which(aaa==0.6) > integer(0) > >> sessionInfo() > R version 2.12.2 (2011-02-25) > Platform: i386-pc-mingw32/i386 (32-bit) > > Am I missing something? Any thoughts?