Re: [R] Defining functions inside loops

2011-02-15 Thread Eduardo de Oliveira Horta
Yes, that was it. Thanks a lot. Best regards, Eduardo On Tue, Feb 15, 2011 at 2:34 PM, jim holtman wrote: > The try this: > >> s <- c( 0.2, 0.45, 0.38, 0.9) >> f  <- lapply(1:10, function(i) > +     local({ > +             local_s <- s[i] > +             function(x)x^2+local_s > +           }))

Re: [R] Defining functions inside loops

2011-02-15 Thread jim holtman
The try this: > s <- c( 0.2, 0.45, 0.38, 0.9) > f <- lapply(1:10, function(i) + local({ + local_s <- s[i] + function(x)x^2+local_s + })) > rm(s) > f[[2]](4) [1] 16.45 > On Tue, Feb 15, 2011 at 10:53 AM, Eduardo de Oliveira Horta wrote: > Thanks... but I g

Re: [R] Defining functions inside loops

2011-02-15 Thread Dr. Matthias Kohl
Dear Eduardo, try: f <- function(x){} s <- 0.2 body(f) <- substitute({x^2 + s}, list(s = s)) Best, Matthias On 15.02.2011 16:53, Eduardo de Oliveira Horta wrote: Thanks... but I guess I didn't make myself clear. What I was trying to do was precisely to "store" inside the function the number as

Re: [R] Defining functions inside loops

2011-02-15 Thread Eduardo de Oliveira Horta
Thanks... but I guess I didn't make myself clear. What I was trying to do was precisely to "store" inside the function the number associated to s[i] rather than the call to s[i], such that I wouldn't need to keep that object in subsequent function calls. In other words, I wanted to use lapply to g

Re: [R] Defining functions inside loops

2011-02-15 Thread jim holtman
You can also create a local copy of 's' in the function: > s <- c( 0.2, 0.45, 0.38, 0.9) > f <- lapply(1:10, function(i) + local({ force(i) + local_s <- s + function(x)x^2+local_s[i] + })) > rm(s) > f[[2]](4) [1] 16.45 > On Tue, Feb 15, 2011 at 12:50 AM, Ed

Re: [R] Defining functions inside loops

2011-02-15 Thread Dennis Murphy
Hi: If you look at the error message, you'll see that you removed s before evaluating f, and since an element of s is called in the function Try > s <- c( 0.2, 0.45, 0.38, 0.9) > f <- lapply(1:10, function(i)local({ force(i) ; function(x)x^2+s[i]})) > f[[1]](s) [1] 0.2400 0.4025 0.3444 1.010

Re: [R] Defining functions inside loops

2011-02-14 Thread Eduardo de Oliveira Horta
Hello again. Let me try something a little more intricate. Let's say instead of forcing evaluation of 'i' I'd want to force evaluation of a vector; for example: s <- c( 0.2, 0.45, 0.38, 0.9) f <- lapply(1:10, function(i)local({ force(i) ; function(x)x^2+s[i]})) rm(s) f[[1]](0.1) Error in f[[1]](0

Re: [R] Defining functions inside loops

2010-11-15 Thread Duncan Murdoch
On 15/11/2010 4:10 PM, William Dunlap wrote: You could make f[[i]] be function(t)t^2+i for i in 1:10 with f<- lapply(1:10, function(i)local({ force(i) ; function(x)x^2+i})) After that we get the correct results > f[[7]](100:103) [1] 10007 10208 10411 10616 but looking at the func

Re: [R] Defining functions inside loops

2010-11-15 Thread Eduardo de Oliveira Horta
Thanks a lot for your readiness! Problem (apparently) solved! Best regards, Eduardo Horta On Mon, Nov 15, 2010 at 7:10 PM, William Dunlap wrote: > You could make f[[i]] be function(t)t^2+i for i in 1:10 > with > f <- lapply(1:10, function(i)local({ force(i) ; function(x)x^2+i})) > After th

Re: [R] Defining functions inside loops

2010-11-15 Thread William Dunlap
You could make f[[i]] be function(t)t^2+i for i in 1:10 with f <- lapply(1:10, function(i)local({ force(i) ; function(x)x^2+i})) After that we get the correct results > f[[7]](100:103) [1] 10007 10208 10411 10616 but looking at the function doesn't immdiately tell you what 'i' is in th

Re: [R] Defining functions inside loops

2010-11-15 Thread Greg Snow
This is a side effect of the lazy evaluation done in functions. Look at the help page for the force function for more details and how to force evaluation and solve your problem. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111