Replace NAN for V_avg

I am writing a procedure that will replace my NaN values for an average value of my data. So far, the program I have written simply copies the previous wave that I had into my new string. This is what my procedure looks like, hopefully someone can tell me if they see anything wrong:

Function replace_nan(data_wave)
Wave data_wave
Variable i

String destName = NameofWave(data_wave)[0,11]+"_modified"
Make/O/N=(numpnts(data_Wave))/D $destName
Wave/D dest = $destName
dest = NaN

Wavestats/q data_wave

    For (i=0; i<(numpnts(data_wave)); i+=1)
            If (data_wave[i]!=NaN)
            dest[i]=data_wave[i]
            Elseif (data_wave[i]== NaN)
            dest[i]=V_avg
        Endif
    Endfor

appendtotable dest
   
End  

First of all, instead of data_wave[i]!=NaN and data_wave[i]== NaN, use the numtype function. That would give you this:
Function replace_nan(data_wave)
    Wave data_wave
    Variable i
 
    String destName = NameofWave(data_wave)[0,11]+"_modified"
    Make/O/N=(numpnts(data_Wave))/D $destName
    Wave/D dest = $destName
    dest = NaN
 
    Wavestats/q data_wave
 
    For (i=0; i<(numpnts(data_wave)); i+=1)
        If (numtype(data_wave[i]) != 2)
            dest[i]=data_wave[i]
        Else
            dest[i]=V_avg
        Endif
    Endfor
 
    appendtotable dest
 
End  


Second, you don't need an explicit for loop here. Instead, you could use waveform assignment. Waveform assignment is much faster than explicit for loops. That leaves you with this:
Function replace_nan(data_wave)
    Wave data_wave
 
    String destName = NameofWave(data_wave)[0,11]+"_modified"
    Duplicate/O data_wave, $destName
    Wave/D dest = $destName
 
    Wavestats/q data_wave
   
    dest = (numtype(data_wave[p]) != 2) ? data_wave[p] : V_avg
 
    appendtotable dest
 
End  

Note that I also replaced the Make command with a simpler Duplicate command.

Third, in many cases, there's a way to use MatrixOp to give you improved performance and often simpler syntax. That's true in your case, giving you this:
Function replace_nan(data_wave)
    Wave data_wave
 
    String destName = NameofWave(data_wave)[0,11]+"_modified"
   
    Wavestats/q data_wave
    MatrixOp/O  $destName = replaceNaNs(data_wave, V_avg)
    Wave/D dest = $destName
   
    appendtotable dest
 End  
The expression: "data_wave[i]== NaN" is not doing what you expect. Have a look at DisplayHelptopic "NumType".
Instead of a loop you can use:


Function replace_nan1(data_wave)
    Wave data_wave
    Variable i
 
    String destName = NameofWave(data_wave)[0,11]+"_modified"
    Make/O/N=(numpnts(data_Wave))/D $destName
    Wave/D dest = $destName
    dest = NaN
 
    Wavestats/q data_wave
    print V_avg
   
    dest = numtype(data_wave) == 2 ? V_avg : data_wave
   
End  



Check out DisplayHelptopic "? :" to see what is happening.


EDIT: Too late ;-)
You could also use poor mans isNaN

Function isNaN(val)
    return !(val == val)
End


and exploit the fact that comparing NaN to everything is false. But I must strongly support Adams suggestions, especially the matrixOP solution. Which is faster, better readable and shorter.