[R] Match() on raw objects ?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jul 27 13:04:32 CEST 2010


On Tue, 27 Jul 2010, bruno Piguet wrote:

> Dear Prof Ripley,
> 
> You wrote :
>       You failed to supply a raw value to match, and if you do it
>       works:
>
>             match(as.raw(2), a)
>
>       [1] 9
>
>       for the ninth value is *not* '2, indeed': it is as.raw(2).
> 
> 
> Thanks for the clarification.
>
>       I think you are confusing R objects with their printed
>       representation.
> 
> 
>    Yes, I must admit. And in this case I was confused by the fact that my
> implementation of a replacement function doing a step-by-step search worked
> without any need of as.raw()
> 
> my_match <- function (needle, hay_stack) {
>   for (i in seq(length(hay_stack)))
>   {
>     if (hay_stack[i] == needle) {
>       return(i)
>     }
>   }
>   return(NA)
> }
> 
> So, even if the ninth value is *not* '2, but as.raw(2), the comparison "a[9]
> == 2" is true.

Correct.  That coerces to integer.

>       (In this case values are coerced to strings, and a[9] is thus
>       coerced to "02", which is not matched by the integer 02 nor the
>       byte "\002".)
> 
> 
>    OK.  Had I tried "02", it would have worked, but only by chance.

No, not 'by chance': that is what the help page for match() tells you 
happens.

>    For my personal guidance, is there a search function that does not coerce
> to string, and thus would be faster ?
>    (even if I must admit that, currently match(as.raw(), ) is fast enough
> and about three times faster than my very naïve replacement).

Depends on how many values you want to look up.

Note that raw values in R are really intended to be passed around and 
not manipulated: if you do much of the latter, coercing to integer, 
say, is likely to be much more efficient.

> Bruno.
> 
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list