[R] odd behaviour of identical

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Oct 27 00:21:03 CET 2008


it's the assignment y[11] = 11 that causes y to become num:

y = 1:10
is(y) # integer vector numeric

y[11] = 11
is(y) # numeric vector

y = (1:11)[1:10]
is(y) # integer vector numeric

anyway, i think this should be considered a bug.  the conversion is
irrational in this case.

this touches another issue discussed before, that of literals like '1'
not being treated as representing integers:

is(1) # numeric vector
is(1:1) # integer vector numeric

thus 1:10 is a vector of integers, but 11 is not an integer, and y[11] =
11 makes y not an integer vector.
one could actually defend r along these lines, but then there is an
inconsistency here.

help(":") says, among others, about the value of an expression of the
form 'from:to':

     For numeric arguments, a numeric vector.  This will be of type
     'integer' if 'from' and 'to' are both integers and representable
     in the integer type, otherwise of type 'numeric'.

well, since is(1) does not report 1 to be an integer (even if the value
is representable as an integer), then 1:1 should not evalluate to an
integer vector.

the problem here is the notorious confusion between integers as numbers
with integers as representations of numbers, both in the docs and in the
design;  the issue has been discussed before. 

i think that from the point of view of most r users, this would be
consistent:

is(1) # integer vector numeric
is(1:1) # integer vector numeric

or this:

is(1) # numeric vector
is(1:1) # numeric vector


i find the following confused:

is(1.0) # numeric vector
is(1.0:1.0) # integer vector numeric


and the following a bug:

is(1.0:1.1) # integer vector numeric

(1.1 is certainly *not* an integer and certainly *not* representable in
an integer type)


and back to the original problem, even though you can explain it away
with the implicit conversion, i still find the behaviour of identical
irrational, since:

identical(1, 1.0)
[1] TRUE

so that there should be nothing wrong in identical(x,y) returning TRUE
after y = y[1:10].

vQ




markleeds at verizon.net wrote:
> the str function shows that x is an int and y is  a num so it's
> probably not a bug. or  maybe  the conversion to num is but probably
> not the identical.
>
> x = 1:10
> y = 1:10
>
> all.equal(x,y)
> identical(x,y)
>
> y[11] = 11
> y = y[1:10]
>
> all.equal(x,y)
> identical(x,y)
>
> print(str(y))
> print(str(x))



More information about the R-help mailing list