[R] I don't understand this

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Sep 3 12:37:17 CEST 2003

"Richard A. O'Keefe" <ok at cs.otago.ac.nz> writes:

> Thomas Lumley <tlumley at u.washington.edu> wrote:
> 	I would have said that the behavior of
> 	   (if (cond) names else dim)(x) <- 10
> 	is undefined in the S language, along with things like the order of
> 	evaluation of the apply functions.
> The thing is, it would make perfect sense for
>     (if (cond) f else g)(x) <- e
> to have the effect of
>     (if (cond) f(x) <- e else g(x) <- e)	    
> In fact, I've used two programming languages where the analogue of
> this _did_ work, and a third where it didn't but using only published
> interfaces could very easily be made to work.

Yes, and it could be made to work in R too with a simple semantic
change: The fundamental problem is that replacement functions are
bound to symbols in R, not to the actual functions. That is the reason
why we have

> x <- c(a=1)
> x
> (names)(x)
[1] "a"
> (names)(x) <- 1
Error: couldn't find function "PT<-"

(that's with the bug unfixed). The thing is that (names) is an unnamed
function so there's no way to know that "names<-" is the replacment
function. If names<- had been an attribute of names, then there would
be no problem. Usual story: Code that needs to go via the textual
representation of something is often subtly wrong.

Of course noone really need to do this, do they? So the incentive to
change is rather small.

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

More information about the R-help mailing list