From macrakis at alum.mit.edu  Tue Mar 10 18:35:59 2009
From: macrakis at alum.mit.edu (Stavros Macrakis)
Date: Tue, 10 Mar 2009 13:35:59 -0400
Subject: [Rd] surprising behaviour of names<-
In-Reply-To: <49B69E52.7050402@idi.ntnu.no>
References: <49B67CB1.8060005@idi.ntnu.no> <49B682F9.8050307@biostat.ku.dk>
	<49B69E52.7050402@idi.ntnu.no>
Message-ID: <8b356f880903101035l5e9a6a1bk3abdd3537801c7b0@mail.gmail.com>

>> (B) you cannot (easily) predict whether or not x will be modified
>> destructively
>
> that's fine, thanks, but i must be terribly stupid as i do not see how
> this explains the examples above. ?where is the x used by something else
> in the first example, so that 'names<-'(x, 'foo') does *not* modify x
> destructively, while it does in the other cases?
>
> i just can't see how your explanation fits the examples -- it probably
> does, but i beg you show it explicitly.

I think the following shows what Peter was referring to:

In this case, there is only one pointer to the value of x:

x <- c(1,2)
> "names<-"(x,"foo")
 foo <NA>
   1    2
> x
 foo <NA>
   1    2

In this case, there are two:

> x <- c(1,2)
> y <- x
> "names<-"(x,"foo")
 foo <NA>
   1    2
> x
[1] 1 2
> y
[1] 1 2

It seems as though `names<-` and the like cannot be treated as R
functions (which do not modify their arguments) but as special
internal routines which do sometimes modify their arguments.

          -s



[Rd] surprising behaviour of names<-
Stavros Macrakis