[Rd] names<- bug, and incompatibility with c() (PR#2358)

Mark.Bravington@csiro.au Mark.Bravington@csiro.au
Tue Dec 10 06:29:03 2002

When assigning names to particular elements of hitherto-unnamed vectors,
there is inconsistent behaviour depending on whether the element being named
is the last one:

test> mm <- 1:3
test> names( mm)[1] <- 'y'
Error in "names<-.default"(*tmp*, value = "y") : 
        names attribute must be the same length as the vector
test> names( mm)[2] <- 'y'
Error in "names<-.default"(*tmp*, value = "y") : 
        names attribute must be the same length as the vector
test> names( mm)[3] <- 'y'
test> mm
<NA> <NA>    y 
   1    2    3 
As long as the last element of mm gets mentioned, R will allow the
operation, filling in any blanks with NA-strings. But if the last element of
mm isn't specified somewhere in the vector of things-to-be-named, R won't
allow the operation.

If mm already has names, there's no problem.

The usage above is sometimes valuable, and it should be easy to fix it, by
supplying default names in all other positions. But there is also some
inconsistency with the use of "c": should default names be NA-strings, or
empty strings? 

test> mm <- 1:2
test> names( mm)[2] <- 'y'
test> mm
<NA>    y 
   1    2 
test> mm <- c( mm, 3)
test> mm
NA  y    
 1  2  3 
test> names( mm)[1]
[1] "NA"

 Note that "c" has supplied an empty string for the unspecified name, and
has also taken the liberty of changing the special NA-string into a "NA"
(i.e. a 2-character string, first letter N, second letter A)!
An easy way to achieve consistency, would be to make unsupplied names should
always default to an empty string, rather than a special NA-string.

--please do not edit the information below--

 platform = i386-pc-mingw32
 arch = i386
 os = mingw32
 system = i386, mingw32
 status = 
 major = 1
 minor = 6.1
 year = 2002
 month = 11
 day = 01
 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