[R] Searching for specific values in a matrix

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Jul 28 00:16:31 CEST 2009


Ahh ..

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)

This isn't working because the numbers you see for y (40.42468) isn't  
precisely what that number is. As I mentioned before you should use an  
"almost.equals" type of search for this scenario. My "%~%" function  
isn't working in your session because that is a function I've defined  
myself. You can of course use it, you just have to define it in your  
workspace. Paste these lines into your workspace (or save them to a  
file and "source" that file into your workspace).

## === almost.equal functions ====

almost.equal <- function(x, y, tolerance=.Machine$double.eps^0.5) {
   abs(x - y) < tolerance
}

"%~%" <- function(x, y) almost.equal(x, y)

## === end paste ==============

Now you can use %~% once that's in. Let's use the almost.equal  
function now because I don't know if the default tolerance here is too  
strict (I suspect showing the value for rearranged$y[1] will show you  
more significant digits than you're seeing in the table(?))

query <- almost.equal(rearranged$y, 40.42468, tolerance=0.0001)
rearranged[query,]

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).

Yes, I believe I mentioned earlier that you have to axe the NA matches  
manually:

query <- rearranged$VSCAT.001 == 464 & !is.na(rearranged$VSCAT.001)
rearranged[query,]

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?

Rounding errors won't happen with integer comparisons (and it looks  
like the VSCAT.001 columns is integers, no?).

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list