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

2012-04-10 Thread R. Michael Weylandt
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

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

2012-04-10 Thread C W
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) wrote: > The error message should make it pretty clear -- you aren't inside a > function so you can't return()

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

2012-04-10 Thread R. Michael Weylandt
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 her