An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120801/697cd869/attachment.pl>
Help with NaN when 0 divided by 0
2 messages · Jennifer Sabatier, William Dunlap
"%/0%" <- function(x,y) { res <- x / y ; res[ is.na(res) ] <- 0;
return(res) }
I think this would be more to the point if the output were set to 0 if the numerator were 0, not if the output would be NA. res[x==0] <-0 You may also want to restructure your computations so that the proportion resulting from 0/0 remains as NaN but when you multiply the proportion by a total you set the result to be 0 if the total is 0. (If the total is not 0 you may want to signal an error.) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jennifer Sabatier Sent: Wednesday, August 01, 2012 7:18 AM To: David Winsemius Cc: r-help at r-project.org Subject: Re: [R] Help with NaN when 0 divided by 0 Hi Everyone, Thanks so much for all your suggestions! All of these worked but David's was best suited for my purposes, considering it was something happens sporadically. I don't do expenditure analyses often as I mostly do run of the mill survey analysis, but this will come in handy for the once or twice a year I do this. Jen On Tue, Jul 31, 2012 at 5:19 PM, David Winsemius <dwinsemius at comcast.net>wrote:
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-
PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html <http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD Alameda, CA, USA
[[alternative HTML version deleted]]