When using arrays in for loops, you may experience incomplete looping for various reasons (bad data, system failure, etc.). When using large numbers of loops and/or when some task within the loop takes a large amount of time, you may consider saving your objects at the end of each loop. If the something goes wrong, you can pick up where you left off by changing the counters in the for loops.
Here is an example. Say you have a 3 by 2 by 2 array. You want the number 2 in the second column of each 3 by 2 submatrix, but only if the corresponding row in the first column has a zero in it. Here we intentionally put a 1 in the second row of the first column of the second matrix to simulate a crash.
# create and save an arbitrary array
temp.A <- array(c(0),c(3,2,2))
save(temp.A,file="temp.A")
# you can use the save/load functions to save loop work
temp.A[2,1,2] <- 1
for(i in 1:3)
{
for(j in 1:2)
{
# save your counters so you can restart you loop
istop <- i
jstop <- j
# you want 2's in the second column of the submatrices
if(temp.A[i,1,j]==0){temp.A[i,2,j]<-2}
# save the array in case your loop crashes
save(temp.A,file="temp.A")
# simulate the loss of temp.A as an internal onject
if(temp.A[i,1,j]!=0)
{
rm(temp.A)
stop("you data is not feasible\n")
}
}
}
# view your 'crash' statistics
istop
jstop
Now that we have had our crash and lost the array as an internal object, we can simulate finding and fixing the error then restarting the loop using our 'crash' statistics. You may consider writing crash statistics to a file as well or printing them to the screen.
# load the saved array
load("temp.A")
# this simulates you fixing the error in your file
temp.A[2,1,2] <- 0
# this loop picks up where the prior loop left off
for(i in istop:3)
{
for(j in jstop:2)
{
if(temp.A[i,1,j]==0){temp.A[i,2,j]<-2}
if(temp.A[i,1,j]!=0){stop("you data is not feasible\n")}
# make your counters local so you can restart you loop
# save your counters so you can restart you loop
istop <- i
jstop <- j
# save the array in case your loop crashes
save(temp.A,file="temp.A")
}
}
# view your array
temp.A