1

I would like to refer to the nth-1 entry in a vector which gets enlarged in each iteration of a loop. How do I refer to this element without having to be specific about its position in the vector?

if I put for example:

vector <- c(0)
T <- 100
for (time in 1:T){
  vector <- cbind(vector, sample(0:1,1))
}

calling up the vector gives me 100 random binary integers . So far so good.

vector

How do I print (or otherwise use) the nth-1 entry from the bottom each time the loop runs through?

Thanks in advance!

Okay, after some time I found the solution. In case anyone is interested:

vector <- c()
T <- 5
for (time in 1:T){
  vector <- cbind(vector, sample(-1:1,1))
  print(vector[,(length(vector)-1)])
}

solves the issue. It prints the last four numbers of the vector.

0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.