-2

my question is that , what is the best way or the right way to deal with NaN and NA and Inf to calculate mean in R:

  • change Inf to NA also and as is.Na(NaN) is TRUE also, simply use the na.rm= TRUEin mean Function or

  • change all to zero and then calculate the mean function.

values that I want to calculate mean for are the values comes from measuring conductance and expansion for community detection algorithm as defined here

Thanks

f.a
  • 101
  • 1
  • 5
  • 14
  • 1
    Well changing them to zero gives you a different result. `mean(c(1,NA,8,9,NA), na.rm=TRUE) # [1] 6; mean(c(1,0,8,9,0)) # [1] 3.6` so I'd probably use `na.rm` – Rich Scriven Nov 23 '14 at 19:53
  • thanks for your comment @RichardScriven yes,my question is that which one is better and why? ... why would you use na.rm ? – f.a Nov 23 '14 at 20:01
  • Because `NA` means "Not Available". That does not mean that value is zero. We don't know what it is. It means that element should be excluded from calculations. We cannot assume that it is zero. It's like someone leaving out an answer on a survey. Would you fill in that answer for them if they choose to omit it? – Rich Scriven Nov 23 '14 at 20:02
  • @RichardScriven, consider that after some calculation you get Na , NaN and Inf in your values , to me is that you are omitting that value and not considering it at all but comsidering your point as well I don't know what is the best way ?! – f.a Nov 23 '14 at 20:05
  • @RichardScriven what about `inf` and `NaN` ? – f.a Nov 23 '14 at 20:07
  • `NaN` means "Not A Number" and infinity is a mathematical value. You'll have to make some decisions. The question is a bit broad and is also likely to attract differing opinions. – Rich Scriven Nov 23 '14 at 20:08
  • @RichardScriven well thanks, i will edit the question to be more specific. – f.a Nov 23 '14 at 20:11
  • The answer to this depends on what you're trying to do, and it's not really a programming question. – Joshua Ulrich Nov 23 '14 at 20:12
  • Here you go, this is useful http://stackoverflow.com/questions/7518245/one-function-to-detect-nan-na-inf-inf-etc?rq=1 – Rich Scriven Nov 23 '14 at 20:16
  • thanks @RichardScriven, I do see that link but my question is more of a conceptual question – f.a Nov 23 '14 at 20:28

1 Answers1

2

Well, I would distinguish between the cases of NA/NaN/Infinity and the rest. I would certainly not convert them to zero as this would distort the result significantly while at the same time, not having any real mathematical sense.

If a value is NA, then it is not, as the name suggests, available If it is NaN, then it is not a number And Inf... well, it's infinity.

In all these cases you cannot get an average. Exclude them, and perhaps try to see why they appear (if you can, have to, need to, etc).

Nikos
  • 3,267
  • 1
  • 25
  • 32