[R] Error using return() function inside for loop and if statement
R. Michael Weylandt
michael.weylandt at gmail.com
Wed Apr 11 05:26:23 CEST 2012
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