Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For loops and vectors in R I have a vector of four elements (denoted vector) and

ID: 3735173 • 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:

sim.output and sim2 are previously defined functions. The function sim2 returns ans1 and ans2. I want to extract ans1 as shown below. I believe the problem is with the syntax, specifically the placement of the i's below in the code.

answer = c()

otherfunc = c()

vector = c(3, 5, 7, 10)

for(i in 1:length(vector))

{

thefunction = sim.output(20, 3, vector[i], 2) #want to use 3, 5, 7, 10 as values in this sim.output

otherfunc[i] = sim2(thefunction, 40) #then use the output from above as one of the arguments in my second function

answer[i] = otherfunc$ans1 #outputs the first element from function above in sim2

print(answer)

}

Explanation / Answer

The error is take place due to function missing. sim.output and sim2 functions be not available in the code. Once you contain that code you be able to run the R code.

Within this line erfunc = sim2(sim.output, 40) , you include not either passed the arguments for sim.output function otherwise you contain to utilize the function value from the preceding line.

You require to define answer vector too previous to the function line answer=c().