Hi Spencer,
On 07/19/2012 08:29 PM, Spencer Graves wrote:
Hello, All:
Do you know of any capability to substitute more then one byte in
an object of class Raw?
Consider the following:
> let4 <- paste(letters[1:4], collapse='')
> (let4Raw <- charToRaw(let4))
> (let. <- sub('bc', '--', let4Raw))
> # no substitution
> (bc <- charToRaw('bc'))
> (ef <- charToRaw('ef'))
> (let. <- sub(bc, ef, let4Raw))
[1] "61" "65" "63" "64"
Warning messages:
1: In sub(bc, ef, let4Raw) :
argument 'pattern' has length > 1 and only the first element will be
used
2: In sub(bc, ef, let4Raw) :
argument 'replacement' has length > 1 and only the first element will
be used
It makes no sense to use sub(), grep(), and family (i.e. all the stuff
based on the regex code) *directly* on a raw vector because all these
functions will start by coercing their 'x', 'text', 'pattern',
'replacement' args to character with as.character (if they are not
already character).
But the way as.character() operates on a raw vector won't give good
results in that context. You'd rather do the coercion yourself first
with rawToChar(), and coerce back the result with charToRaw():
> charToRaw(sub("bc", "--", rawToChar(let4Raw)))
[1] 61 2d 2d 64
IMO it would make much more sense that sub(), grep(), and family()
raise an error than blindly try to coerce to character but these
functions (like many functions in R) are too polite to tell the
user s/he's doing something wrong.