Charles Dupont
2007-Jan-16 18:24 UTC
[Rd] Why does not the command 'length(a <- 1:5) <- 4' not work?
when running the command > length(a <- 1:5) <- 4 there are two responses. If 'a' does not exist then the response is Error in length(a <- 1:5) <- 4 : object "a" not found If 'a' does exist then the response is Error in length(a <- 1:5) <- 4 : could not find function "<-<-" I would assume that 'length(a <- 1:5) <- 4' should work because 'length(a <- 1:5)' does work. Thank you for your help. Charles Dupont -- Charles Dupont Computer System Analyst School of Medicine Department of Biostatistics Vanderbilt University
Duncan Murdoch
2007-Jan-16 19:12 UTC
[Rd] Why does not the command 'length(a <- 1:5) <- 4' not work?
On 1/16/2007 1:24 PM, Charles Dupont wrote:> when running the command > > length(a <- 1:5) <- 4 > > there are two responses. > > If 'a' does not exist then the response is > > Error in length(a <- 1:5) <- 4 : object "a" not found > > If 'a' does exist then the response is > > Error in length(a <- 1:5) <- 4 : could not find function "<-<-" > > I would assume that 'length(a <- 1:5) <- 4' should work because > 'length(a <- 1:5)' does work.I'm guessing you are assuming it would mean the same as a <- 1:5 length(a) <- 4 But how would R know the name of the variable whose length is set in the second line? In "a <- 1:5" the "a" is just part of a larger expression, it's not special as in some other languages, i.e. R sees that as "<-"(a, 1:5) So if you rewrote your original as length("<-"(a, 1:5)) <- 4 it is a lot less clear that you really mean to create and then change "a". In general things like foo(a) <- b are evaluated as "foo<-"(a, b), where "foo<-" is a function that expects a variable reference as its first argument. There's some magic going on to allow things like a <- matrix(1:4, 2,2) names(dim(a)) <- letters[1:2] and I think the parser is trying to set things up for that kind of magic in your expression, though I haven't traced through the execution path to explain exactly why you saw the error messages you saw. Duncan Murdoch
Thomas Lumley
2007-Jan-16 19:26 UTC
[Rd] Why does not the command 'length(a <- 1:5) <- 4' not work?
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. Consider length(names(a))<-5 and suppose names(a) is c("A","B"). This code doesn't just evaluate names(a) and pass the result on, since length(c("A","B"))<-5 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) so length(names(a))<-5 is 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 length(a)<-4 then just do it like that. -thomas