-1

I am trying to create a basic foraging model and at the moment I get outputs such as:

repeats= 1 time= 19 : food eaten= 100 
repeats= 2 time= 33 : food eaten= 100 
repeats= 3 time= 1001 : food eaten= 100

for each loop, the output I am interested in is the "time".

Is there an easy way to create a table which would store repeats and time?

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Would be helpful to see what is happening within "for loop", how are you getting "time" ? – zx8754 Nov 22 '16 at 12:37
  • sorry its my first time posting and I have very basic knowledge of R, the repeats are from a simple for loop "for(tt in 1:10){ }" around the whole code, the time out put is the result of a while loop "while(food.eaten<100){ time <- time+1}" – Gareth Pool Nov 22 '16 at 12:39
  • Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Nov 22 '16 at 12:41

1 Answers1

0

I don't know if this is close to what you're looking for. It's a loop and the loop# and time are stored in a dataframe:

loops <- 5
repeats <- vector("numeric", loops)
time <- vector("numeric", loops)
start <- Sys.time()
for(i in seq_along(repeats)) {

  repeats[i] <- i
  Sys.sleep(1) # This suspends execution for a second
  time[i] <- Sys.time() - start
}

results <- cbind(repeats, time)
results
     repeats     time
[1,]       1 0.989223
[2,]       2 2.029239
[3,]       3 3.018046
[4,]       4 4.034053
[5,]       5 5.049059

I added in a 1 second suspension per loop so that a difference in time would be obvious in the output.

William
  • 166
  • 10