>>>>> "BertG" == Bert Gunter <gunter.ber...@gene.com> >>>>> on Mon, 27 Jul 2009 15:30:08 -0700 writes:
BertG> Nothing wrong with rolling your own, but see BertG> ?all.equal for R's built-in "almost.equal" version. Yes, indeed! Note that that needs a little extra care, as it either returns TRUE or a character vector. In sum, I'd strongly suggest you use %~% <- function(x,y) isTRUE(all.equal(x,y)) Martin Maechler, ETH Zurich and R Core Team BertG> Bert Gunter Genentech Nonclinical Biostatistics BertG> -----Original Message----- From: BertG> r-help-boun...@r-project.org BertG> [mailto:r-help-boun...@r-project.org] On Behalf Of BertG> Steve Lianoglou Sent: Monday, July 27, 2009 3:17 PM BertG> To: Mehdi Khan Cc: r-help@r-project.org Subject: Re: BertG> [R] Searching for specific values in a matrix BertG> Ahh .. BertG> On Jul 27, 2009, at 6:01 PM, Mehdi Khan wrote: >> Even when choosing a value from the first few rows, it >> doesn't work. okay here it goes: >> >> > rearranged[1:10, 1:5] x y band1 VSCAT.001 soiltype 1 >> -124.3949 40.42468 NA NA CD 2 -124.3463 40.27358 NA NA CD >> 3 -124.3357 40.25226 NA NA CD 4 -124.3663 40.40241 NA NA >> CD 5 -124.3674 40.49810 NA NA CD 6 -124.3083 40.24744 NA >> 464 <NA> 7 -124.3017 40.31295 NA NA D 8 -124.3375 >> 40.47557 NA 464 <NA> 9 -124.2511 40.11697 1 NA <NA> 10 >> -124.2532 40.12640 1 NA <NA> >> >> > query<- rearranged$y== 40.42468 > rearranged[query,] >> [1] x y band1 VSCAT.001 soiltype <0 rows> (or 0-length >> row.names) BertG> This isn't working because the numbers you see for y BertG> (40.42468) isn't precisely what that number is. As I BertG> mentioned before you should use an "almost.equals" BertG> type of search for this scenario. My "%~%" function BertG> isn't working in your session because that is a BertG> function I've defined myself. You can of course use BertG> it, you just have to define it in your BertG> workspace. Paste these lines into your workspace (or BertG> save them to a file and "source" that file into your BertG> workspace). BertG> ## === almost.equal functions ==== BertG> almost.equal <- function(x, y, BertG> tolerance=.Machine$double.eps^0.5) { abs(x - y) < BertG> tolerance } BertG> "%~%" <- function(x, y) almost.equal(x, y) BertG> ## === end paste ============== BertG> Now you can use %~% once that's in. Let's use the BertG> almost.equal function now because I don't know if the BertG> default tolerance here is too strict (I suspect BertG> showing the value for rearranged$y[1] will show you BertG> more significant digits than you're seeing in the BertG> table(?)) BertG> query <- almost.equal(rearranged$y, 40.42468, BertG> tolerance=0.0001) rearranged[query,] BertG> This will get you something. >> query<- rearranged$ VSCAT.001== 464 except it's a huge >> table (I guess I have to get rid of all rows with NA). BertG> Yes, I believe I mentioned earlier that you have to BertG> axe the NA matches manually: BertG> query <- rearranged$VSCAT.001 == 464 & BertG> !is.na(rearranged$VSCAT.001) rearranged[query,] BertG> Will get you what you want. >> I tried using the %~% but R doesn't recognize it. So >> maybe it has to do with the rounding errors? BertG> Rounding errors won't happen with integer comparisons BertG> (and it looks like the VSCAT.001 columns is integers, BertG> no?). BertG> -steve BertG> -- Steve Lianoglou Graduate Student: Computational BertG> Systems Biology | Memorial Sloan-Kettering Cancer BertG> Center | Weill Medical College of Cornell University BertG> Contact Info: http://cbio.mskcc.org/~lianos/contact BertG> ______________________________________________ BertG> R-help@r-project.org mailing list BertG> https://stat.ethz.ch/mailman/listinfo/r-help PLEASE BertG> do read the posting guide BertG> http://www.R-project.org/posting-guide.html and BertG> provide commented, minimal, self-contained, BertG> reproducible code. BertG> ______________________________________________ BertG> R-help@r-project.org mailing list BertG> https://stat.ethz.ch/mailman/listinfo/r-help PLEASE BertG> do read the posting guide BertG> http://www.R-project.org/posting-guide.html and BertG> provide commented, minimal, self-contained, BertG> reproducible code. ______________________________________________ R-help@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.