Skip to content
Prev 301898 / 398503 Next

Help with NaN when 0 divided by 0

On Jul 31, 2012, at 1:23 PM, Jennifer Sabatier wrote:

            
It's possible to define new infix operators (although I have forgotten  
which help page describes this in more detail and I cannot seem to  
find it right now):

  "%/0%" <- function(x,y) { res <- x / y ; res[ is.na(res) ] <- 0;  
return(res) }

# You cannot use %/% because it is already used for integer division.  
I guess you could use "//", but to me that looks too much like "||"  
which is the single-value-OR. You could also use "%div0%".

  var1 %/0% var2

#[1]   0.00000  13.88889  53.79000   0.00000 150.00000 350.00000

If this is a regular need, you can put this in a .profile file or a  
package. See:

?Startup