alejandro andrade alejandro andrade - 18 days ago 6
R Question

How to save the output of a loop that increases by a sequence

So i'm trying to save the output of a loop that is going 5 by 5 instead of 1 by 1.
The code i have is:

for ( i in seq(40,200,by=5)){
test=StatArb(Col,Bog,i,100,1.1)
print(test[2])
}


I created the
function StatArb
and the output is a list of 6 elements. I want the second one which is a single number. Instead of printing the result I want it to store it in a vector. I have tried creating a vector and replacing each i entry by the result of the test, but it doesn't work. The code is:

vector = rep(0,32)
for ( i in seq(40,200,by=5)){
test=StatArb(Col,Bog,i,100,1.1)
vector[i] = test[i]
}


But for some reason in the loop it expands the vector from 32 to 200 and so I get a lot of Null numbers.

G5W G5W
Answer

How about sapply(seq(40,200,5), function(i) { StatArb(Col,Bog,i,100,1.1)[2]})