Skip to content
Prev 17892 / 63424 Next

Subscripting fails if name of element is "" (PR#8161)

On Thu, 6 Oct 2005, "Jens Oehlschl?gel" wrote:

            
I think Thomas is right as to how S interprets this: "" is no name on 
assignment, wheread NA as a name is a different thing (there probably is a 
name, we just do not know what it is).

Here is the crux of the example.

p <- c(a=1, 2)
[1] "a" ""
a
1 2
[1] TRUE

so giving the name is "" really is the same as giving no name.

`Error 1' is said to be
<NA>
   NA

You haven't given a name, so I think that is right.  S (which has no 
character NAs) uses "" as the name, but here there may be a name or not.
I think Jens then meant as `error 2' that
$a
[1] 1

[[2]]
[1] 2

shows no name for the second element, and that seems right to me (although 
S shows "" here).

Finally (`error 3')
$"NA"
NULL

is a length-one list with name character-NA.  (S has no name here.)  That 
seems the right answer but if so is printed inconsistently.

I would say that
$a
[1] 1

$"NA"
[1] 2

was the only bug here (the name should be printed as <NA>).  Now that
comes from this bit of code

 		    if( isValidName(CHAR(PRINTNAME(TAG(s)))) )
 			sprintf(ptag, "$%s", CHAR(PRINTNAME(TAG(s))));
 		    else
 			sprintf(ptag, "$\"%s\"", CHAR(PRINTNAME(TAG(s))));

so non-syntactic names are printed surrounded by "".  Nowadays I think we 
would prefer ``, as in
$"a+b"
[1] 1
[1] 1
[1] 1

but NA needs to be a special case as in
$"NA"
[1] 1

$"NA"
[1] 2
[1] FALSE  TRUE