[R] Help with NaN when 0 divided by 0

Marc Schwartz marc_schwartz at me.com
Tue Jul 31 22:36:03 CEST 2012


On Jul 31, 2012, at 3:23 PM, Jennifer Sabatier <plessthanpointohfive at gmail.com> wrote:

> Hi All,
> 
> I have some data where I am doing fairly simple calculations, nothing more
> than adding, subtracting, multiplying and dividing.
> 
> I’m running into a problem when I divide one variable by another and when
> they’re both 0 I get NaN. I realize that if you divide a non-zero by 0 then
> you get Inf, which is, of course, correct.  But in my case I never get Inf,
> just NaN because of the structure of my dataset.
> 
> 
> Here’s a dumb example:
> 
> 
> 
> var1 <- c(0, 500, 5379, 0, 1500, 1750)
> 
> var2 <- c(0, 36, 100, 0, 10, 5)
> 
> 
> var1/var2
> 
> 
> I realize the NaNs are logical, but for my purposes this should just be 0
> because I am calculating expenditures and if you spent no money in one
> sub-area and none in the whole area then you don't have an expenditure at
> all, so it should be 0.  And since R doesn't like adding NA's or NaN's to
> anything, I'd rather just have this be 0 so that my future calculations,
> such as adding up expenditure, is simple.
> 
> Is there an easy way to avoid the NaN's, something a non-programmer (ie,
> the person I am handing this code off to) would understand?
> 
> Thanks,
> 
> 
> Jen

You could use ?ifelse:

> ifelse(var2 == 0, 0, var1 / var2)
[1]   0.00000  13.88889  53.79000   0.00000 150.00000 350.00000

It is very common in programming to include code to handle exceptions, so don't be shy about using conditional coding as may be appropriate.

Regards,

Marc Schwartz



More information about the R-help mailing list