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 [[alternative HTML version deleted]]
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
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, > > > JenYou 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
On Jul 31, 2012, at 1:23 PM, Jennifer Sabatier 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 >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 --> 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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD Alameda, CA, USA