5
$\begingroup$
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.

$\endgroup$
14
  • 1
    $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Mar 31, 2017 at 19:30
  • 1
    $\begingroup$ Yes, I typed it in. $\endgroup$ Mar 31, 2017 at 19:33
  • 3
    $\begingroup$ "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!" $\endgroup$
    – JimB
    Mar 31, 2017 at 19:35
  • 3
    $\begingroup$ 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. $\endgroup$ Mar 31, 2017 at 21:58
  • 2
    $\begingroup$ ...or, you can use the eminently more readable Missing[] if you're uncomfortable with Indeterminate. $\endgroup$ Apr 1, 2017 at 0:37

4 Answers 4

7
$\begingroup$

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

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

or

list /. NaN -> Nothing // Mean

2

$\endgroup$
2
  • 3
    $\begingroup$ 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: 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. $\endgroup$
    – Szabolcs
    Mar 31, 2017 at 19:32
  • 2
    $\begingroup$ Good to know about this new thing of Nothing. $\endgroup$
    – Ruslan
    Apr 1, 2017 at 13:57
10
$\begingroup$

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.

$\endgroup$
6
$\begingroup$
Mean@Cases[{1, 2, 3, NaN, 2}, Except[NaN]]
(* 2 *)

Mean@DeleteCases[{1, 2, 3, NaN, 2}, NaN]
(* 2 *)
$\endgroup$
1
$\begingroup$
Solve[Mean[{2, nan, 1, 4}] == nan]

Or

Solve[Mean[list] == nan]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.