Skip to content
Prev 173793 / 398503 Next

Help plase with simple loop

I don't think you have gotten the basic idea of functions in R. Those  
functions would return only the value of the last evaluated  
expression, e.g. (1+par[1]) in the case of f1, but most of the code  
appears to have no effect. See this simple example:

 > x1 <- 4
 > f1 <- function(x) x1 <- x
 > f1(1)
 > x1
[1] 4  # nothing happens to the x1 in the enclosing environment,; the  
one inside the function is gone.

The "x1" inside the function "f1" is local to the function and has no  
persistency after the evaluations are completed..

f4 <- function(par) {1+par[4]} # would have same effect as your f1
 > f4(1:4)
[1] 5

You should tell us what you actually want to do. You generally call  
functions with a particular set of arguments for which you have  
provided no examples and then expect those functions to return some  
sort of object which you have also not defined.