In R, I am using Min and Max to find minimum and maximum values for a given vector.

This vector is a result of computation and filter and often times could be NULL or empty list.

In such case, we get warnings such as:

Warning message: In max(el) : no non-missing arguments to max; returning -Inf

If you do this inside a loop (or ply) for multiple inputs, you would start encountering big list of warnings

The default value of Inf is not suitable for my purposes and would like to instead have 0 or some other value to be returned.

Now, apart from every time checking if the input is valid with if..else conditions, is there any way I can stipulate the default return value for these Min, Max methods in case of empty inputs.

For example, if you use na.rm=TRUE then it would be difficult to pre-check the input if it would become empty after NA is removed.

Another case would be min(which()). checking return value of which() first and then supplying conditionally to min() is not really something I want.

Also, post-processing the Min, Max results to manually remove/replace Inf with my own values is not really efficient way.

So, is there any efficient way of asking R to return me my own custom default value from Min, Max for empty vectors, instead of +Inf and -Inf ??

The pre-processing with if..else checks and post-processing with Inf replacement are not so elegant.

If writing custom wrapper around Min, Max is the only way, how would you suggest write it without compromising on speed or elegance?

Similar questions and discussions