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

Mark.Bravington@csiro.au Mark.Bravington@csiro.au
Tue Dec 10 23:32:02 2002


#> From: Mark.Bravington@csiro.au 

#> When assigning names to parts of an hitherto-unnamed vector,
#> 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

#From: ripley@stats.ox.ac.uk [mailto:ripley@stats.ox.ac.uk]

#Subject: Re: [Rd] names<- bug, and incompatibility with c() (PR#2358)
#
#
#This is S-compatible, though, and it I think it is quite legitimately
#an error.  I will fix it in R-devel, but I am little concerned that
#advanced users may be surprised (but they probably would never 
#do this).

don't be so sure! The "names" attribute can be useful for many things
besides the obvious-- "There are more things in heaven and earth,
Horatio,..."   ;)

#>  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)!
#
#That's a bug.

If you use NA-strings for unsupplied names, then will you for consistency be
making another change to c() so that

> mm <- c( a=1, b=2)
> mm <- c( mm, 5)

now gives an NA-string for mm's 3rd name? 

#
#> 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.
#
#But what's the point of having a special string then?

That's just what I was wondering (in the specific case of names attributes).
I can see the logic of NA-string names, but I do wonder whether this will
cause more practical problems than just using an empty string. It's not just
back-compatibility, but also unexpected behaviour. For example, after
setting up mm as above:

test> mm
<NA> <NA>    y 
   1    2    3 
test> names( mm)=='y'
[1]   NA   NA TRUE
test> mm[ names(mm)=='y']
<NA> <NA>    y 
  NA   NA    3 

the last line being a bit surprising to me, anyway. It's not a bug, but will
it be helpful? Empty names instead of NA-names give a behaviour that seems
to me more "natural". 

cheers
Mark

*******************************

Mark Bravington
CSIRO (CMIS)
PO Box 1538
Castray Esplanade
Hobart
TAS 7001

phone (61) 3 6232 5118
fax (61) 3 6232 5012