0
$\begingroup$

How can I plot the mean of such a list: http://pastebin.com/dNjeR4WC which contains an alternating number of elements in the sublists and some sublists that are empty and should not be considered in the plot (the corresponding mean does not exist).

Example:

{{},{},{},{1,2},{1,2,3},{4,5},{3,4,5,6,7},{},{2,3,4,5,6},{6}}

For the upper example I would like to plot:

{{},{},{},Mean[{1,2}],Mean[{1,2,3}],Mean[{4,5}],Mean[{3,4,5,6,7}],{},
Mean[{2,3,4,5,6}],Mean[{6}]}
$\endgroup$
12
  • 1
    $\begingroup$ Whatever does the mean of such a list mean? The mean of each nonempty list? The mean of all the numbers in that list? Some clarity would be appreciated. $\endgroup$ Commented Jul 15, 2016 at 13:24
  • 1
    $\begingroup$ So what about {{}, {}, {}, {1, 2}, {1, 2, 3}, {4, 5}, {3, 4, 5, 6, 7}, {}, {2, 3, 4, 5, 6}, {6}} // DeleteCases[{}] // Map[Mean] // ListPlot? $\endgroup$
    – Jason B.
    Commented Jul 15, 2016 at 13:31
  • 1
    $\begingroup$ So, If[# =!= {}, Mean[#], #] & /@ list? $\endgroup$ Commented Jul 15, 2016 at 13:32
  • 1
    $\begingroup$ Like Table[Mean[a[[i]]], {i, Length[a]}] /. Mean[{}] -> {}? $\endgroup$
    – Feyre
    Commented Jul 15, 2016 at 13:38
  • 1
    $\begingroup$ ListLinePlot[{{}, {}, {}, 3/2, 2, 9/2, 5, {}, 4, 6} /. {} -> Missing[]]? $\endgroup$ Commented Jul 15, 2016 at 13:40

2 Answers 2

3
$\begingroup$
list = {{}, {}, {}, {1, 2}, {1, 2, 3}, {4, 5}, {3, 4, 5, 6, 
    7}, {}, {2, 3, 4, 5, 6}, {6}};
list // MapIndexed[If[#1 != {}, {First@#2, Mean@#1}] &] // 
  DeleteCases[Null] // ListPlot

Mathematica graphics

Here it is with the larger data set

<< "http://pastebin.com/raw/dNjeR4WC" // 
   MapIndexed[If[#1 != {}, {First@#2, Mean@#1}] &] // 
  DeleteCases[Null] // ListPlot

Or, with a slightly shorter syntax (thanks to J.M. for pointing it out)

<< "http://pastebin.com/raw/dNjeR4WC" // Map[Mean] // 
  ReplaceAll[_Mean :> Missing] // ListPlot

both of which give the same result:

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Can you show it also for the pastebin data? $\endgroup$
    – mrz
    Commented Jul 15, 2016 at 13:57
  • $\begingroup$ Sure thing! (minimum character count for comment reached now) $\endgroup$
    – Jason B.
    Commented Jul 15, 2016 at 14:03
1
$\begingroup$

Example

data = {{}, {}, {}, {1, 2}, {1, 2, 3}, {4, 5}, {3, 4, 5, 6, 7}, {}, {2, 3, 4, 5, 6}, {6}}
Mean /@ Select[data, UnsameQ[#, {}] &]

Alternativly, here is a version proposed by @JasonB

data // Select[Not@*EqualTo[{}]] // Map[Mean]

Output

{3/2, 2, 9/2, 5, 4, 6}

$\endgroup$
2
  • $\begingroup$ It's slightly longer than your version, but I'm really enjoying using postfix notation with operator forms, so you could write that as data // Select[Not@*EqualTo[{}]] // Map[Mean] $\endgroup$
    – Jason B.
    Commented Jul 15, 2016 at 14:14
  • $\begingroup$ Thanks @JasonB, I will add it to this answer as an alternative form! I am still trying to wrap my hand around all the syntax nuances in wolfram language. I find it particularly mind bending when writing code following rule-based approach: s $\endgroup$ Commented Jul 15, 2016 at 14:17

Your Answer

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

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