Quickly get the mean of a wave with NaN values

In python, I can do

average_despite_NaN = numpy.nanmean(data)

One line, no problem. In Igor, the mean() command says "If any values in the point range are NaN, mean returns NaN."

I need to quickly compare average values of several waves, each with random NaN values. In the data browser, I can look at the wave and it will tell me its average, even though there are NaNs. How come the mean() function can't do that, and what's the proper function?

WaveStats will ignore NaN values when calculating the average (stored in the V_avg output variable). For more information, execute:

DisplayHelpTopic "WaveStats"

If you only need the mean (and/or a few other statistics) you can use the /M=1 flag. That will make the calculation faster.

I would use

Function dostuff()

    Make/FREE wv = {1, 2, NaN, 4, 5}           
    MatrixOP/FREE avg = mean(zapNans(wv)))
    print avg
End

which gives you the expected result of three and is just one line. The nice thing about matrixOP is that it is composable, so calcuatling the max/min/sin etc. can be done with the same approach.

One caveat: This requires the current beta version of Igor Pro 9.

Here is a (somewhat convoluted) one-liner doing the same, should you still be stuck with Igor 8:

Function dostuff()
    Make/FREE wv = {1, 2, NaN, 4, 5}          
    MatrixOP/FREE avg = sum(replaceNaNs(wv,0)) / sum(equal(numType(wv),0))
    print avg
End

This MatrixOp line calculates the sum of your wave without the NaNs and than divides it with the number of all non-NaN elements, which gives the mean.