[R] Unexpected result of names<-

Liaw, Andy andy_liaw at merck.com
Wed Nov 16 18:34:13 CET 2005


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

> z["aaa"]
$aaa
[1] 1

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




More information about the R-help mailing list