# [R] incoherent conversions from/to raw

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu Mar 19 00:26:55 CET 2009

```i wonder about the following examples showing incoherence in how type
conversions are done in r:

x = TRUE

x[2] = as.raw(1)
# Error in x[2] = as.raw(1) :
#  incompatible types (from raw to logical) in subassignment type fix

it seems that there is an attempt to coerce the raw value to logical
here, which fails, even though

as.logical(as.raw(1))
# TRUE

likewise,

x[2] = 1L
# the vector is silently coerced upwards to integer
x[2] = as.raw(1)
# Error in x[2] = as.raw(1) :
#  incompatible types (from raw to integer) in subassignment type fix

even though

as.integer(as.raw(1))
# 1

and likewise for double and complex.

there's another incoherence:

x = 1
x[2] = 1i
x
# 1i 1i

x = 1i
x[2] = 1
x
# 1i 1i

in both cases, the higher type is used for the result;  in the former
case, the vector is coerced upwards, in the latter, the assigned value
is coerced upwards.  however:

x = 1
x[2] = as.raw(1)
# error: incompatible types (from raw to double)

x = as.raw(1)
x[2] = 1
# error: incompatible types (from double to raw)

leaving aside that

as.double(as.raw(1))
# 1
as.raw(as.double(1))
# 1

work just fine, in both cases there is an attempt to coerce the assigned
value to the vector type, and not to the higher type (which would
presumably qbe double, as in ?c), as in the previous example.

interestingly,

c(1, as.raw(1))
# error: type 'raw' is unimplemented in 'RealAnswer'

(note the 'real', not 'double'), whereas

1 == as.raw(1)
# TRUE

works just fine.  furthermore,

c('1', as.raw(1))
# "1" "01"

whereas

x = '1'
x[2] = as.raw(1)
# error: incompatible types (from raw to character)

yet another issue is that of indexing a raw vector with an out-of-bounds
index.  the r language definition, sec. 3.4.1 [1] says:

"
We shall discuss indexing of simple vectors first. For simplicity,
assume that the expression is x[i]. (...)
If i is positive and exceeds length(x) then the corresponding selection
is NA.
"

it's probably correct to assume that 'simple vector' means 'atomic
vector', though not all r core members seem to be quite sure [2]:

"
So what is a simple vector? That is not explicitly defined, and it
probably should be. I think it is "atomic vectors, except those with a
class that has a method for [".
"

it appears that raw vectors are atomic vectors:

is.atomic(as.raw(1))
# TRUE

so an index out of bounds (at least, a positive integer index exceeding
the length of the vector) should (?) produce an NA;  however,

as.raw(1)[2]
# 00

this is presumably because there is no raw NA, and an NA of whatever
type is converted to 0 by as.raw:

as.raw(NA)
# 00
# warning: out-of-range values treated as 0 in coercion to raw

but in this case there's a warning.  why does not out-of-bounds indexing
of a raw vector not produce a warning?  following the language
definition, the fact that a raw vector is atomic, and the above informal
statement on simple and atomic vectors, the selection should first
produce an NA, which only subsequently is coerced to the raw 0 -- with a
warning.  there's an analogous issue with out-of-bounds assignment:

x = as.raw(1)
x[3] = as.raw(3)
x
# 01 00 03

but

x = 1
x[3] = 3
x
# 1 NA 3
as.raw(x)
# warning: out-of-range values treated as 0 in coercion to raw

is all this an intended feature?

vQ

[1] http://cran.r-project.org/doc/manuals/R-lang.html#Indexing-by-vectors
[2] http://tolstoy.newcastle.edu.au/R/e6/devel/09/03/0954.html

```