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
> + }))
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
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
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
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
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
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
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
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
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
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
11 matches
Mail list logo