Skip to content

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

4 messages · Jens Oehlschlägel, Brian Ripley, Duncan Murdoch

#
Dear Thomas,
I respectfully disagree: the element has a name, its an empty string. Of
course "" is a doubtful name for an element, but as long as we allow this
name when assigning names()<- we also should handle it like a name in
subscripting. The alternative would be to disallow "" in names at all.
However, both alternatives rather look like code changes, not only
documentation. 

Best regards


Jens Oehlschl?gel
#
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
#
I haven't been following this conversation in order, but I think there's 
another bug here besides the one(s?) you identified:

Jens had this example:

 > x <- 1:4
 > names(x) <- c(NA, "NA", "a", "")
 > x[names(x)]
<NA> <NA>    a <NA>
    1    1    3   NA

Shouldn't the second entry in the result be 2, with name "NA"?  It seems 
the string "NA" has been converted to <NA> here.

Duncan Murdoch
Prof Brian Ripley wrote:
#
On Fri, 7 Oct 2005, Duncan Murdoch wrote:

            
Yes, but I don't see it in PR#8161 where there is no name "NA" that I can
see.  (In other words it is not an instance of the subject line.)

The issue is that <NA> is matching "NA", and it should not.  As in the
code

Rboolean NonNullStringMatch(SEXP s, SEXP t)
{
     if (CHAR(s)[0] && CHAR(t)[0] && strcmp(CHAR(s), CHAR(t)) == 0)
 	return TRUE;
     else
 	return FALSE;
}

and there are more instances around.