From berwin at maths.uwa.edu.au  Thu Mar 12 10:32:31 2009
From: berwin at maths.uwa.edu.au (Berwin A Turlach)
Date: Thu, 12 Mar 2009 17:32:31 +0800
Subject: [Rd] surprising behaviour of names<-
In-Reply-To: <49B8D060.8090206@idi.ntnu.no>
References: <49B67CB1.8060005@idi.ntnu.no> <49B682F9.8050307@biostat.ku.dk>
	<49B6E6D9.7010809@idi.ntnu.no>
	<96A4BB14-D6EA-44B1-B524-6DB139F3A7F8@r-project.org>
	<8CCAAAD3-919F-462F-A616-F4FDDB46D975@r-project.org>
	<49B81186.4070107@idi.ntnu.no>
	<20090312154008.56706f38@berwin-nus1>
	<49B8D060.8090206@idi.ntnu.no>
Message-ID: <20090312173231.7a2929df@berwin-nus1>

On Thu, 12 Mar 2009 10:05:36 +0100
Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:

> well, as far as i remember, it has been said on this list that in r
> the infix syntax is equivalent to the prefix syntax, [...]

Whoever said that must have been at that moment not as precise as he or
she could have been.  Also, R does not behave according to what people
say on this list (which is good, because some times people they wrong
things on this list) but according to how it is documented to do; at
least that is what people on this list (and others) say. :)

And the R Language manual (ignoring for the moment that it is a draft
and all that), clearly states that 

	names(x) <- c("a","b")

is equivalent to
	
	'*tmp*' <- x
         x <- "names<-"('*tmp*', value=c("a","b"))

[...]
> well, i can imagine a user using the prefix 'names<-' precisely under
> the assumption that it will perform functionally;  

You mean
	y <- 'names<-'(x, "foo")
instead of
	y <- x
	names(y) <- "foo"
?

Fair enough.  But I would still prefer the latter version this it is
(for me) easier to read and to decipher the intention of the code.

> i.e., 'names<-'(x, 'foo') will always produce a copy of x with the
> new names, and never change the x.  

I am not sure whether R ever behaved in that way, but as Peter pointed
out, this would be quite undesirable from a memory management and
performance point of view.  Image that every time you modify a (name)
component of a large object a new copy of that object is created.
 
> cheers, and thanks for the discussion.

You are welcome.

Cheers,

	Berwin



[Rd] surprising behaviour of names<-
Berwin A Turlach