[Rd] unwanted coercion of length 0 vectors (PR#2587)

Mark.Bravington@csiro.au Mark.Bravington@csiro.au
Thu Feb 27 04:11:03 2003


When something is assigned to an element of a vector that was previously of
length 0, the vector is coerced to the mode of the something, regardless of
whether the coercion is necessary under normal R rules.

In particular, if NA is assigned to an element of a vector that was
previously length 0, the mode of the vector is coerced to 'logical'.

For example, here's the normal behaviour:

test> x1_ 5
test> x1[1]_ NA
test> x1
[1] NA
test> mode( x1)
[1] "numeric"

No coercion was required because 'logical' (the default mode of NA) is
"ancestral" to 'numeric'. Note also that assigning to a hitherto
non-existent element of x1, e.g. x1[2]_ NA, also does not coerce.

But:

test> x0_ numeric( 0)
test> x0[1]_ NA
test> mode( x0)
[1] "logical"

Similar things happen with vectors of mode 'complex' and even 'character':

test> xc0_ complex( 0)
test> xc0[1]_ 3
test> mode( xc0)
[1] "numeric" # should be 'complex'

test> xch0_ character(0)
test> xch0[ 1]_ 3
test> mode( xch0)
[1] "numeric"

Intriguingly, something strange happens even with lists:

test> xl1_ list( 1)
test> xl1[1]_ 3
test> xl1
[[1]]
[1] 3
test> xl0_ list()
test> xl0[1]_ 3
Error: (list) object cannot be coerced to vector type 14

cheers
Mark

mark.bravington@csiro.au

--please do not edit the information below--

Version:
 platform = i386-pc-mingw32
 arch = i386
 os = mingw32
 system = i386, mingw32
 status = 
 major = 1
 minor = 6.2
 year = 2003
 month = 01
 day = 10
 language = R

Windows 2000 Professional (build 2195) Service Pack 2.0

Search Path:
 .GlobalEnv, ROOT, package:handy, package:debug, mvb.session.info,
package:mvbutils, package:tcltk, Autoloads, package:base