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: 3734872 • 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(n))

{

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

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

answer[i] = otherfunc$ans1 #takes in the first element of return from the function above

print(answer)

}

Explanation / Answer

here i have done some modificatins please check once,

answer = c()

otherfunc = c()

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

for(i in 1:length(vector))

{

thefunction = sim.output(20, 3, vector[i], 2)

otherfunc[i] = sim2(sim.output, 40)

answer[i] = otherfunc[1]

print(answer)

}