I have a function in R, say
f1 <- function(x,y,vec, func0,...){
...
...
...
return(out)
}
The arguments func0 and vec in this function f1 are function object and some vector object respectively. Now I want to repeat this function 'reps' times (everything else being the same). I have stored the arguments of this function in a list as there are a lot of arguments and I keep changing them to do the replications again.
list1 <- list(x,y,vec, func0, other arguments)
Then I want to do, f1_reps <- lapply(1:reps, f1, list1)
I get an error when I do this as the function arguments func0 and vec are not found.
Any help in this direction would be helpful. Here is a mock example of the situation.
Here is an example,
a <- function(n){
sqrt(n)
}
N = 100
out <- rep(NA,N)
# simple function with multiple arguments
foo <- function(a=a, b= c(1:3), c= 1000){
for(n in 1:N){
out[n] <- b%*%b+ a(n)*c
}
return(out)
}
candidates <- list(a=a, b = c(1:3), c=1000)
lapply(1:4, foo(a=candidates$a,b=candidates$b,c=candidates$c)) ## Doesn't work
lapply(1:4, foo, a=candidates$a, b=candidates$b, c=candidates$c) ## Doesn't work
candidates2 <- c(a=a, b = c(1:3), c=1000) # A vector of arguments
lapply(1:4, foo, a=candidates2$a, b = c(candidates2$b1,candidates2$b2,candidates2$b3), c=candidates2$c) #Doesn't work either