[Rd] surprising behaviour of names<-

Berwin A Turlach berwin at maths.uwa.edu.au
Thu Mar 12 10:32:31 CET 2009


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



More information about the R-devel mailing list