user1234440
user1234440

Reputation: 23577

Warning Message Line Number R

I've got a big for loop that loops for hundreds of times and at its end it produces this warning:

Warning messages:
1: In min(j, na.rm = TRUE) :
  no non-missing arguments to min; returning Inf

Is there any way I could ask R which line the warning message was generated at?


OP's comment below: "I don't directly have min as a line. Its probably nested in other functions else I wouldn't have asked the question as I knew it was a problem coming from min."

Upvotes: 12

Views: 6385

Answers (2)

NPE
NPE

Reputation: 500663

You could try setting:

options(warn = 2)

... to treat warnings as errors. Then, when your code stops at the first warning, use traceback() to see the stack trace.

This will only help you with the first warning though.

To go back to the default behaviour, use:

options(warn = 0)

Upvotes: 18

Ricardo Saporta
Ricardo Saporta

Reputation: 55380

This is a basic for loop howto, not really R dependent

Right before your min line put

 print(paste("j is", j, "\n"))  # or instead of j, use i, or whichever index you are using     
 min(j, na.rm = TRUE) 

then you will have a good idea of where the error is.


As for a more R relevant solution, if j is coming from a data.frame, matrix, list, etc,
you want to find which chunk (iteration portion) has nothing but NAs.

For that you can use something like

  apply(myDF, 1, function(x) all(is.na(x)))

Upvotes: 2

Related Questions