I have a code in R that reads, one line at the time, through a data.frame and if a certain set of conditions is met, changes the value of one of the variables in the data.frame. In pseudo code:
for(i in 1:nrow(data)) {
if (conditions on data[i,]) { change value } else {do nothing}
}
While the code is running, at a certain point it stops and throws the following error message: Error in if (condition : missing value where TRUE/FALSE needed
I understand that the error message means that, at a certain point, when the condition in the if
statement is evaluated the result is Na
rather than a TRUE
or FALSE
.
However, when I try the condition in R by using the value of i
that is "stored" in R (and which I assume to be the row of the data set that throws the error) I get an answer of TRUE
. Do I understand correctly that the value of i
allows me to identify which line of the data frame is throwing the error? If not, should I look for some other way to identify which row of the data set is causing the error?
As long as your for loop is not inside a function, i will be equal to the final value it hit before the error. Thus after your error:
Should give you the pathological row.
If you are running inside a function, due to scoping rules, i should die with the function. In that case, I would modify your code to print out every line (or i) until it dies: