From murdoch at stats.uwo.ca  Fri May 23 20:48:52 2008
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Fri, 23 May 2008 14:48:52 -0400
Subject: [Rd] names<- bug or feature?
In-Reply-To: <48370B63.4040309@pburns.seanet.com>
References: <48370B63.4040309@pburns.seanet.com>
Message-ID: <48371194.4060808@stats.uwo.ca>

On 5/23/2008 2:22 PM, Patrick Burns wrote:
> The two statements below with 'names' are conceptually
> the same.  The second performs the desired operation
> while the first does not.
> 
>  > xx <- list(A=c(a=1, b=2))
>  > names(xx$A[1]) <- "changed"
>  > xx
> $A
> a b
> 1 2
> 
>  > names(xx$A)[1] <- "changed"
>  > xx
> $A
> changed       b
>       1       2
> 
> This is observed in 2.4.0 on Linux as well as 2.7.0 and
> 2.8.0  on Windows XP.

Those aren't the same.  The first one says to extract the first element 
of xx$A and set its names.  The second says to set the first name of xx$A.

Remember that names(x) <- y is the same as

*tmp* <- x
x <- `names<-`(*tmp*, y)

which in your first case expands to

*tmp* <- xx$A[1]
xx$A[1] <- `names<-`(*tmp*, "changed")

But assigning a named number to a numeric vector has no effect on the 
names.  It would be like

xx <- c(a=1, b=2)
xx[1] <- c(d=1)

which has no effect on xx.

Duncan Murdoch



[Rd] names<- bug or feature?
Duncan Murdoch