Zero divison in R

r

Solution

Well, let's back up a minute. R does NOT return an error. It quite correctly returns NaN. You had better have a darn good reason for rejecting NaN values in your work. Why do you allow any `b` element to be zero in the first place? You need to think about what your code is really intended to do, and what it means (statistically, say) to cheerfully throw away all the cases where `b[j]==0` .

Problem

Is there an easy way of avoiding 0 division error in R. Specifically, ``` a <- c(1,0,2,0) b <- c(3,2,1,0) sum(b/a) ``` This code gives an error due to division by zero. I would like a way to define anything/0 = 0 so that this kind of operation would still be valid.

Original source