Message-ID: <CAM_vjumL2fSmH9QnmJqoG_wGyba0OhnJ68WhyiuPbvVj7jxzxg@mail.gmail.com>
Date: 2012-07-31T20:35:20Z
From: Sarah Goslee
Subject: Help with NaN when 0 divided by 0
In-Reply-To: <CAOxgQ=Ufius3DDypgrZ4ZUnMK_gRnefQp1R7_1Ob2rY6RCP5Kw@mail.gmail.com>
Hi,
Here are two possible ways to deal with it. Which is better depends on
the larger context of your code. There's no right way, just whichever
is more convenient.
> ifelse(var2 != 0, var1/var2, 0)
[1] 0.00000 13.88889 53.79000 0.00000 150.00000 350.00000
>
> newvar <- var1/var2
> newvar[is.nan(newvar)] <- 0
> newvar
[1] 0.00000 13.88889 53.79000 0.00000 150.00000 350.00000
>
Sarah
On Tue, Jul 31, 2012 at 4: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
>
--
Sarah Goslee
http://www.functionaldiversity.org