[R] Indexing, and using an empty string as a name

Charilaos Skiadas cskiadas at gmail.com
Wed Nov 7 13:39:32 CET 2007


Sorry, I meant to reply to the whole list.

I can totally understand the list's policy of not defaulting to  
"reply to the list", but I keep forgetting it in practice (since I am  
in a couple of other lists with less traffic, where the default is to  
reply to the list.

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

My off-list reply follows.
On Nov 7, 2007, at 7:35 AM, Charilaos Skiadas wrote:

> On Nov 7, 2007, at 2:51 AM, Prof Brian Ripley wrote:
>
>> On Tue, 6 Nov 2007, Charilaos Skiadas wrote:
>>
>>> Hello all,
>>>
>>> I ran into the following, to me unexpected, behavior. I have (for
>>> reasons that don't necessarily pertain to the question at hand,  
>>> hence
>>> I won't go into them) the need/desire to use an empty string for the
>>> name of a vector entry.
>>
>> The 'R Languge Definition' says
>>
>>   The string "" is treated specially: it indicates `no name' and  
>> matches
>>   no element (not even those without a name).
>>
>> and from ?names
>>
>>   The name \code{""} is special: it is used to indicate that there  
>> is no
>>   name associated with an element of a (atomic or generic) vector.
>>   Subscripting by \code{""} will match nothing (not even elements  
>> which
>>   have no name).
>>
>> so it should perhaps have been expected.
>
> It should indeed, if I had looked at ?names, which I should have  
> thought of doing. I wonder why ?"[" doesn't mention it though,  
> especially the part about: "Subscripting by "" will match nothing  
> (not even elements which have no name)."
>
> What are the reasons for treating "" in a special way, btw? i.e.  
> can't we use NA to indicate "no name"? (Hm, come to think of it, NA  
> means more that we don't know the name, not that there isn't a  
> name. So perhaps it makes sense after all.)
>
> Actually the interpretation as "no name" is rather appropriate in  
> my case, in a way. The reason I have empty names is that I wanted  
> to "fake the presence of a grouping variable". I.e. I have code  
> that expect a factor, and uses that factor to split the data and  
> compute summaries. Providing a "trivial" factor would allow the  
> same code to handle the case where there is no factor at all.
>
>>> Perhaps I did not read ?"[" very carefully,
>>> but it seems to me that he following lines should return "1" at  
>>> the end:
>>>
>>> x<-1:4
>>> names(x) <- c("","a","b","c")
>>> x[""]
>>>
>>>
>>> Instead, they return NA. Could anyone provide an explanation for  
>>> this
>>> behavior?
>>
>> You can do
>>
>>> x[[""]]
>> [1] 1
>>
>> (which is arguably a bug given the quotes above) or
>>
>> x[match("", names(x))]
>
> I like this second approach more. I had to use dimnames(table)[3]  
> instead of names(x) (and a couple more commas), since in my actual  
> case those names are in an array, but the idea is the same and it  
> worked beautifully. Thanks!
>
>>
>> -- 
>> Brian D. Ripley,                  ripley at stats.ox.ac.uk
>> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford,             Tel:  +44 1865 272861 (self)
>> 1 South Parks Road,                     +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>
> Haris Skiadas



More information about the R-help mailing list