[R] Is it a bug in list() behavior?

Spencer Graves spencer.graves at pdf.com
Mon Mar 24 20:12:55 CET 2003


This is a difference between S-Plus and R.

S-Plus 6.1 for Windows Professional Ed. Rel. 1:
 > tst <- c(a1 = 1, b2 = 3)
 > tst["a"]
  a1
   1

R 1.6.2:
 > tst <- c(a1=1, b2=3)
 > tst["a"]
<NA>
   NA

This is important for me, because some of my collaborators use S-Plus 
but not R and others use R but not S-Plus.  It's best for me if I can 
adopt a style of use that is maximally transportable.

Best Wishes,
Spencer Graves

Uwe Ligges wrote:
> Tony Plate wrote:
> 
>> As wolski/Eryk's example shows, it seems that "[[" for lists accepts 
>> abbreviations, whereas "[" does not.  Is this intended?  (This is a 
>> difference from S-plus - both "[" and "[[" for lists accept 
>> abbreviations in S-plus (V6.1 for Windows at least.)
> 
> 
> The general subscripting operator [] doesn't support abbreviations at 
> all. I don't know of any reference that states [] supports partial 
> matching of character strings.
> 
> 
>> I couldn't find any mention of this difference in regards to accepting 
>> abbreviations in either ?"[" or section 6.1 of the Introduction to R, 
>> or in the R Language Manual, or in the R Reference Manual.  [As an 
>> aside, I'd rather that the subset operators didn't accept 
>> abbreviations at all,but ...]
> 
> 
> [[]] is the component extractor for lists, and the reference I gave 
> tells us that partial matching works for component indexing.
> I agree that it's a good idea to mention this behaviour in the R 
> Language *Definition* manual.
> 
> 
> 
>> The name returned by "[" for a non-existent element of a list also 
>> seems of dubious correctness.
>>
>>
>>> list(abc=123)[["a"]]
>>
>>
>> [1] 123
>>
>>> list(abc=123)["a"]
>>
>>
>> $"NA"
>> NULL
> 
> 
> Everything as expected from my point of view. Do you mean the "NA" is 
> "dubious"?
> 
> See the R Language Definition, Section 3.4.1:
> "Notice however, that there are different modes of NA—the literal 
> constant is of mode "logical", but it is frequently automatically 
> coerced to other types."
> Remember, it's a name!
> 
> Uwe Ligges
> 
> 
>>
>>> list(abc=123)$a
>>
>>
>> [1] 123
>>
>>> version
>>
>>
>>          _              platform i386-pc-mingw32
>> arch     i386           os       mingw32        system   i386, 
>> mingw32  status                  major    1              minor    
>> 6.2            year     2003           month    01             
>> day      10             language R             
>>
>>
>> At Monday 04:54 PM 3/24/2003 +0100, you wrote:
>>
>>> wolski wrote:
>>>
>>>> Hello!
>>>> let:
>>>> test<-1:3
>>>> list(test)
>>>> names(test)<-c("X11","X12","Y23")
>>>>
>>>>
>>>>> test[["Y2"]]
>>>>
>>>>
>>>> 3
>>>> I had assumed that the names in a list are like a keys in a hash.
>>>> Therefore i thought that no value should be returned.
>>>> The behavior of:
>>>>
>>>>
>>>>> test["Y2"]
>>>>
>>>>
>>>> <NA>   NA is as i expected.
>>>>
>>>> Should it be as it is? How is the definition of [[]] and []?
>>>
>>>
>>> No! See "An Introduction to R", Section 6.1:
>>> "The names of components may be abbreviated down to the minimum 
>>> number of letters needed to identify them uniquely. Thus 
>>> Lst$coefficients may be minimally specified as Lst$coe and 
>>> Lst$covariance as Lst$cov."
>>>
>>> Uwe Ligges
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>>
>>
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help



More information about the R-help mailing list