I have been trying to run this code (below here) and I have gotten that message \"Error in if (temp[ii] == 0) { : missing value where TRUE/FALSE needed\"...
Difficult without knowing the language, but i think the issue is that the value in ii can be greater than the length of temp when i is at its upper bound. I'd have expected an index out of range or something similar but, without knowing the language, who knows! Hope you get your problem fixed.