5
list={1,2,3,NaN,2}

I'd like to calculate the mean of this list, ignoring the NaN. Answer should be 2 if the NaN is ignored. Seems like this should be crazy simple, but I haven't seen anything reasonable. Thanks in advance.

corey979
  • 23,947
  • 7
  • 58
  • 101
Tom Mozdzen
  • 521
  • 5
  • 16
  • What is "NaN" here? How did it get into your list in the first place? – Szabolcs Mar 31 '17 at 19:25
  • OK - original list was list={1,2,3,0,2}. I'd like to ignore all of the values of zero in this case. NaN is the shortcut for "NotANumber". I tried replacing the zero with a NaN, but Mathematica doesn't like that. – Tom Mozdzen Mar 31 '17 at 19:27
  • Matlab handles it this way: A = [1 0 0 1 NaN 1 NaN 0]; M = mean(A,'omitnan') – Tom Mozdzen Mar 31 '17 at 19:28
  • 1
    You did not answer my question. There is no standard symbol called NaN in Mathematica. Where did this list come from? Did you type it in? Was it returned by a library? Please edit your question and make this clear. – Szabolcs Mar 31 '17 at 19:30
  • 1
    Yes, I typed it in. – Tom Mozdzen Mar 31 '17 at 19:33
  • This is called Indeterminate in Mathematica, not NaN. Corey's answer is correct then (except don't use NaN). – Szabolcs Mar 31 '17 at 19:34
  • http://reference.wolfram.com/language/ComputerArithmetic/ref/NaN.html – Tom Mozdzen Mar 31 '17 at 19:34
  • 1
    That is something entirely different, provided by a package. It is not a built-in symbol. – Szabolcs Mar 31 '17 at 19:34
  • 3
    "Yes, I typed it in." This is like the old Henny Youngman joke: "Doc, it hurts when I do this." Doc's response: "Don't do that!" – JimB Mar 31 '17 at 19:35
  • From that reference on NaN: To use NaN, you first need to load the Computer Arithmetic Package using Needs["ComputerArithmetic`"]. Are you using the ComputerArithmetic package? – JimB Mar 31 '17 at 19:44
  • OK, this seems to work for a list, but not a matrix. I'll have to reask the question, this time without the NaN – Tom Mozdzen Mar 31 '17 at 19:55
  • If it's just the mean of all of the non-NaN numbers in a matrix, then throw in a Flatten to any of the solutions below. – JimB Mar 31 '17 at 20:27
  • 3
    Let me just clarify why your question has caused such confusion. Mathematica has a rather unique model of arithmetic and departs from IEEE754 in many important respects. In particular, if you somehow manage to get an actual IEEE754 NaN value into the system, it will give extreme difficulties to deal with it in any consistent and useful way. This is what makes people nervous on seeing your question. For some value that has a meaning like that of NaN but is not an IEEE754 NaN, you can just use the officially sanctioned and supported value of Indeterminate. – Oleksandr R. Mar 31 '17 at 21:58
  • 2
    ...or, you can use the eminently more readable Missing[] if you're uncomfortable with Indeterminate. – J. M.'s missing motivation Apr 01 '17 at 00:37

4 Answers4

10

Well, it is a bit more nuanced in my opinion. It seems like the right functionality is to actually do keep track of the illegal entries in the system, similar to keeping missing variables for the statistical analysis in other packages. If that's the case, the more appropriate way is to actually code them with Missing. Then the computation can be done ignoring such entries, but the list would still retain the structure of the original data and can always be used for any analysis that do care about missing observations.

list = {1, 2, 3, 0, 2, 3, 5};
listnothing = list /. {0 -> Nothing};
listmissing = list /. {0 -> Missing["Not A Number"]};
Mean[listnothing]
Mean[DeleteMissing[listmissing]]

8/3

8/3

So far so good, but compare what happens if you want to plot the lists:

Row[MapThread[
  ListLinePlot[#, PlotLabel -> Style[#2, Bold, 15], 
    ImageSize -> Medium] &, 
    {{list, listnothing, listmissing}, 
    {"Original List", "List with Nothing", "List with Missing"}}]]

Blockquote

Not only the shape is wrong (it show the connection between points that doesn't exist), but also the number of observations is wrong in the case of Nothing, so the more appropriate way for the data processing is to use Missing.

Stitch
  • 4,205
  • 1
  • 12
  • 28
7

If your NaN comes from an external program and is just some symbol, then

Mean @ Select[list, # =!= NaN &]

or

list /. NaN -> Nothing // Mean

2

corey979
  • 23,947
  • 7
  • 58
  • 101
  • 3
    This assumes that OP is talking about a plain old symbol that happens to be called NaN (that he maybe typed in). This is likely the case. But there are dangerous things that look like "NaN" in Mathematica: http://mathematica.stackexchange.com/q/19026/12 Even if he did type it in, he should be aware that in Mathematica, NaN has no special meaning the same way Indeterminate does. – Szabolcs Mar 31 '17 at 19:32
  • 2
    Good to know about this new thing of Nothing. – Ruslan Apr 01 '17 at 13:57
6
Mean@Cases[{1, 2, 3, NaN, 2}, Except[NaN]]
(* 2 *)

Mean@DeleteCases[{1, 2, 3, NaN, 2}, NaN]
(* 2 *)
Ali Hashmi
  • 8,950
  • 4
  • 22
  • 42
1
Solve[Mean[{2, nan, 1, 4}] == nan]

Or

Solve[Mean[list] == nan]
Coolwater
  • 20,257
  • 3
  • 35
  • 64