[R] Unexpected result of names<-

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Nov 16 20:21:14 CET 2005


On Wed, 16 Nov 2005, Liaw, Andy wrote:

> That's because S does partial matching of component names, if it can be 
> done unambiguously:
>
>> z <- list(aaa=1, bbb=2)
>> z$a
> [1] 1
>> z[["a"]]
> [1] 1
>> z["a"]
> $"NA"
> NULL
[That could be done unambiguously, but [] does not partial match in R
(see ?"["), only [[]] and $ do.  That is different from S (Blue Book 
p.358), and I do not know why.]
>> z["aaa"]
> $aaa
> [1] 1

Well, *sometimes*.  For

> z <- list(aaa=1, bbb=2)
> z$a <- 3
> z
$aaa
[1] 1

$bbb
[1] 2

$a
[1] 3

It is that the rules are different for extraction and replacement that 
surprises people.  In this case we have effectively

z <- "$<-"("names<-"(z$a, "X"), "a")

and the replacement function "$<-" does not partially match (Blue Book, 
p.362).


>
> Andy
>
>
>
> From: Hong Ooi
>>
>> Hi,
>>
>> I came across some rather unexpected behaviour the other day with
>> assigning names and lists. Here's an example.
>>
>>> z <- list(aaa=1, bbb=2)
>>> z
>> $aaa
>> [1] 1
>>
>> $bbb
>> [1] 2
>>
>> Note that z has members named "aaa" and "bbb". Now:
>>
>>> names(z$a) <- "X"
>>> z
>> $aaa
>> [1] 1
>>
>> $bbb
>> [1] 2
>>
>> $a
>> X
>> 1
>>
>> I would have expected that trying to name z$a would either
>> give an error
>> (because z doesn't have an element "a") or would cause z$aaa to be
>> modified (due to partial name matching).  I didn't expect that a new
>> list member would be created.
>>
>> I've checked that this is consistent across SPlus 2000, SPlus 7, and R
>> 2.2 for Windows. Can someone give an explanation for why this is
>> happening?
>>
>>
>> --
>> Hong Ooi
>> Senior Research Analyst, IAG Limited
>> 388 George St, Sydney NSW 2000
>> (02) 9292 1566
>>
>>
>> ______________________________________________________________
>> _________________________
>>
>> The information transmitted in this message and its
>> attachments (if any) is intended
>> only for the person or entity to which it is addressed.
>> The message may contain confidential and/or privileged
>> material. Any review,
>> retransmission, dissemination or other use of, or taking of
>> any action in reliance
>> upon this information, by persons or entities other than the
>> intended recipient is
>> prohibited.
>>
>> If you have received this in error, please contact the sender
>> and delete this e-mail
>> and associated material from any computer.
>>
>> The intended recipient of this e-mail may only use,
>> reproduce, disclose or distribute
>> the information contained in this e-mail and any attached
>> files, with the permission
>> of the sender.
>>
>> This message has been scanned for viruses with Symantec Scan
>> Engine and cleared by
>> MailMarshal.
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>>
>>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
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




More information about the R-help mailing list