For loops and vectors in R I have a vector of four elements (denoted vector) and
ID: 3734328 • Letter: F
Question
For loops and vectors in R
I have a vector of four elements (denoted vector) and would like to run a for loop that substitutes each of these values in a function (denoted thefunction) that another function (denoted otherfunc) relies on, then stores the result of this function in a new vector (denoted answer). Example code below that's giving an error:
vector = c(3, 5, 7, 10)
for(i in 1:length(n))
{
thefunction = sim.output(20, 3, vector[i], 2)
otherfunc = sim2(sim.output, 40)
answer[i] = otherfunc$ans1
print(answer)
}
Explanation / Answer
The error is occured due to function missing. sim.output and sim2 functions are not available in the code. Once you have that code you can run the R code.
And in this line erfunc = sim2(sim.output, 40) , you have not either passed the arguments for sim.output function or you have to use the thefunction value from the previous line.
You need to define answer vector also before the function line answer=c().
Please check it and let me know if you have any doubts.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.