Uffe Høgsbro Thygesen
2001-Oct-10 09:13 UTC
[R] Weird feature when creating function lists with apply
Hi R-fellows, can anyone explain this weird feature? I am trying to create a list of functions with apply, and it appears that there is something strange going on when the function evaluates the argument a. When I duplicate a into a local variable b, the result changes !?! Any pointers? Thank in advance. Cheers, Uffe # Create a function which returns a function> f1 <- function(a) {return(function(x) a*x)}# Create a list of functions, parameterised by 1:4> apply(as.array(1:4),1,f1)[[2]](1)[1] 4 # Repeat> f2 <- function(a) {b <- a ;return(function(x) b*x)} > apply(as.array(1:4),1,f2)[[2]](1)[1] 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Uffe H. Thygesen, M.Sc.&Eng., Ph.D. Danish Institute of Fisheries Research http://www.dfu.min.dk -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian Ripley
2001-Oct-10 09:34 UTC
[R] Weird feature when creating function lists with apply
On Wed, 10 Oct 2001, [iso-8859-1] Uffe Høgsbro Thygesen wrote:> Hi R-fellows, > > can anyone explain this weird feature? I am trying to create a list of > functions with apply, and it appears that there is something strange going > on when the function evaluates the argument a. When I duplicate a into a > local variable b, the result changes !?! > > Any pointers? Thank in advance. Cheers, > > Uffe > > > # Create a function which returns a function > > f1 <- function(a) {return(function(x) a*x)} > # Create a list of functions, parameterised by 1:4 > > apply(as.array(1:4),1,f1)[[2]](1) > [1] 4 > > # Repeat > > f2 <- function(a) {b <- a ;return(function(x) b*x)} > > apply(as.array(1:4),1,f2)[[2]](1) > [1] 2I don't think this is what you intended. ll <- apply(as.array(1:4),1,f1) ll [[1]] function(x) a*x <environment: a6d374> [[2]] function(x) a*x <environment: a6d09c> [[3]] function(x) a*x <environment: a6dd60> [[4]] function(x) a*x <environment: a6da88> which is not what your comment implies. And> get("a", envir=environment(ll[[2]]))[1] 4 shows why they differ. 1) This is a strange way to produce a list via arrays: ll <- lapply(1:4, f1) is what is normally used. 2) I think what you really wanted was f3 <- function(a) substitute(function(x) a*x, list(a = a)) lapply(1:4, f3) [[1]] function(x) 1 * x [[2]] function(x) 2 * x [[3]] function(x) 3 * x [[4]] function(x) 4 * x -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2001-Oct-10 09:36 UTC
[R] Weird feature when creating function lists with apply
Uffe H?gsbro Thygesen <uht at dfu.min.dk> writes:> # Create a function which returns a function > > f1 <- function(a) {return(function(x) a*x)} > # Create a list of functions, parameterised by 1:4 > > apply(as.array(1:4),1,f1)[[2]](1) > [1] 4 > > # Repeat > > f2 <- function(a) {b <- a ;return(function(x) b*x)} > > apply(as.array(1:4),1,f2)[[2]](1) > [1] 2This should work too: f3 <- function(a) {a;function(x) a*x} apply(as.array(1:4),1,f3)[[2]](1) It is essentially the same situation that caused Luke Tierney substantial headscratching recently. The key is lazy evaluation. Here's part of the conversation from then: [Snip from email from Luke] On Wed, Aug 29, 2001 at 11:25:54PM +0200, Peter Dalgaard BSA wrote:> Luke Tierney <luke at nokomis.stat.umn.edu> writes: > > > > mkf <- function(j) function() j > > > for (i in 1:10) x[[i]] <- mkf(i) > > > > That doesn't work either: > > > > > x[[2]]() > > [1] 10 > > > > but for a slightly different reason: Lazy evaluation defers evaluating > > the argument to mkf(i) until the funtction is called. > > Argh. I've been looking at this for ten minutes and it still refuses > to sink in....Now imagine getting bitten by this while experimenting with some new threading code, which is of course then the prime suspect for all bad things that happen, and you can see why Duncan and I were tearing our hair out for a bit :-).> > Oh, now I see it: > > > z<-2 ; f<-mkf(z) > > z<-3 > > f() > [1] 3 > > z<-4 > > f() > [1] 3 > > The return value of mkf is not really a function of the argument, it > is the same function every time, namely the function returning "j" > which it will look up in its parent environment (the lexical one...) > when called, and only *then* will it force the promise.-- 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 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Uffe Høgsbro Thygesen
2001-Oct-10 10:49 UTC
[R] Weird feature when creating function lists with apply
Thanks, Peter and Prof. Ripley, for your helpful replies. Referring to Prof. Ripley's reply, when I do f3 <- function(a) substitute(function(x) a*x, list(a = a)) f <- lapply(1:4, f3)[[1]] then f is not a function:> is.function(f)[1] FALSE but> is.function(eval(f))[1] TRUE Consistently, if I do f4 <- function(a) eval(substitute(function(x) a*x, list(a = a))) g <- lapply(1:4, f4)[[1]] then g is indeed a function:> is.function(g)[1] TRUE I do still not understand what f is, then, except something that evaluates to a function. My conclusion is that I need to start my head, too, and understand this lazy evaluation. Thanks again. Cheers, Uffe ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Uffe H. Thygesen, M.Sc.&Eng., Ph.D. Danish Institute of Fisheries Research http://www.dfu.min.dk -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hello All, I am creating a graph in a loop, and want to use letters for marking symbols. Is there a function to convert an integer into a character. I am looking for something similar to the ASC() function of good old BASIC, or the (char) type conversion of C. Thanks, John. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._