[R] Match() on raw objects ?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jul 27 12:28:31 CEST 2010


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

I think you are confusing R objects with their printed representation.
(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".)

On Tue, 27 Jul 2010, bruno Piguet wrote:

> Dear all,
>
>   Sorry to bother you with a question that must be a FAQ, but I'm not
> clever enough to have Google give me the answer.
>
>   I want to filter out some bytes that I read in a file, before applying
> rawToChar (to prevent null character problems)
>
>   So, I tried things like match("<some_byte>", my_raw_tab),   but this
> didn't work.
>
>   Here is a minimal example :
>
> a <- charToRaw(paste("garbage", "\002", "some text", "\003", "garbage"))
> print (a[9])
> i <- match (02, a)
> j <- match ('\002', a)
> print (c(i, j))
>
>  All I get as result of "match" calls are NA's, while print (a[9]) ensures
> me that the ninth value of a is 2, indeed.
>
>   Of course, hand-made search gives me the result :
> for (i in seq(length(a)))
> {
>  if (a[i] == 2) {
>    print (i)
>  }
> }
>
> Any hints ?
>
> Thanks in advance,
>
> Bruno.
> PS : R version 2.11.0 (2010-04-22)
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>

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