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.weyla...@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 <tmrs...@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@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.