Re: [Rd] Why does not the command 'length(a <- 1:5) <- 4' not work?

From: Thomas Lumley <>
Date: Tue 16 Jan 2007 - 19:26:27 GMT

On Tue, 16 Jan 2007, Charles Dupont wrote:

> I would assume that 'length(a <- 1:5) <- 4' should work because
> 'length(a <- 1:5)' does work.

Appearances can be deceptive.

length(a <- 1:5) evaluates a <- 1:5 and then passes its value to length(), in ordinary call-by-value semantics.

That can't be how complex assignment functions work, though.



and suppose names(a) is c("A","B"). This code doesn't just evaluate names(a) and pass the result on, since

would be silly (and invalid). It must be doing something more complicated to make sure that "a", buried deep inside the call, gets updated.

What actually happens is that

   f(x) <- y
is rewritten in terms of the assignment function "f<-" as

   x <- "f<-"(x, y)


   names(a) <- "length<-"(names(a), 5)
which in turn is

   a <- "names<-"(a, "length<-"(names(a), 5))

This requires the assignment function "f<-" to exist, of course.

In your case

   length(a <- 1:5) <- 4
is rewritten as

   (a<-1:5) <- "length<-"(a<-1:5, 5)
and then as

   a <- "<-<-"(a, "length<-"(a<-1:5, 5)) which gives the error you report -- there is no function "<-<-".

You might think the last stage of rewriting could just be avoided, but

   (a<-1:5) <- "length<-"(a<-1:5, 5)
is invalid if not rewritten, since it tries to modify 1:5, which is not a variable.

If you want to compute

    a <- 1:5
then just do it like that.

         -thomas mailing list Received on Wed Jan 17 06:27:57 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 16 Jan 2007 - 21:31:11 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.