[R] Error using return() function inside for loop and if statement

R. Michael Weylandt michael.weylandt at gmail.com
Wed Apr 11 06:12:13 CEST 2012


You don't need to return() from the for loop -- just put your outputs
in a variable:

set.seed(1) # For reproducibility
x <- numeric(20)
for(i in 1:20) x[i] <- bob(0.5, sqrt)

or (more elegant but basically the same thing)

set.seed(1)
x1 <- replicate(20, bob(0.5, sqrt)) # Same calculation done 20x and
results collected

but what's best is to make your code vectorized (which is pretty
straightforward here):

bob <- function(var1, func)
{
   func <- match.fun(func) # Make sure it's a function
  return(ifelse(runif(length(var1)) < var1, var1, func(var1)))
}

set.seed(1)
x2 <- bob(rep(0.5, 20), sqrt)

identical(x, x1) # TRUE
identical(x, x2) # TRUE

Incidentally, you don't need the return() for the simplified version
of bob() because the function only has one possible end-point and, by
default, returns the result of the last calculation.

Best,
Michael

On Tue, Apr 10, 2012 at 11:56 PM, C W <tmrsg11 at gmail.com> wrote:
> Thanks.
>
> So, I want the function to return results from the if statement.
>
> bob <- function(var1, func)
> {
>    #func: a simple function
>
>    num1 <- var1
>    num2 <- func(var1)
>
>    if(ruinf(1)<num1)
>    {
>       return(num1)
>    }else{
>       return(num2)
>    }
> }
>
> And then, run 20 iterations of the function.
>
> On Tue, Apr 10, 2012 at 11:26 PM, R. Michael Weylandt
> <michael.weylandt at gmail.com> wrote:
>>
>> The error message should make it pretty clear -- you aren't inside a
>> function so you can't return() a value which is, by definition, what
>> functions (and only functions) do.** Not sure there's a great
>> reference for this though beyond the error message....
>>
>> Incidentally, what are you trying to do here? The code doesn't really
>> make much sense to me -- do you just want samples = 5*(1:10) ?
>>
>> Michael
>>
>> ** I think there's a subtlety glossed over herein: functional
>> programming purists can wince silently if they so choose.
>>
>> On Tue, Apr 10, 2012 at 11:17 PM, C W <tmrsg11 at gmail.com> wrote:
>> > Dear all, I get an error using the return function.  The following is a
>> > simpler version.
>> >
>> > for (j in 1:10)
>> > {
>> >    samples = 5*j
>> >    return(samples)
>> > }
>> >
>> > Error: no function to return from, jumping to top level
>> >
>> >
>> > Similar warning happens with if statement.
>> >
>> > Why do I get an error?  print() works fine.  I don't see anywhere in the
>> > documentation says that return() is not allowed inside for loop.
>> >
>> > I know the list is for advanced computation topics, and I don't want to
>> > bother the list too much.  Where should I look for things like this in
>> > the
>> > future?
>> >
>> > ?return() nor the R documentation does not mention any of this.
>> >
>> > Bob
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list