# How to plot the mean of a 2d list with some empty elements

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}]}
• 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. Jul 15, 2016 at 13:24
• 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? Jul 15, 2016 at 13:31
• So, If[# =!= {}, Mean[#], #] & /@ list? Jul 15, 2016 at 13:32
• Like Table[Mean[a[[i]]], {i, Length[a]}] /. Mean[{}] -> {}? Jul 15, 2016 at 13:38
• ListLinePlot[{{}, {}, {}, 3/2, 2, 9/2, 5, {}, 4, 6} /. {} -> Missing[]]? Jul 15, 2016 at 13:40

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

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:

• Can you show it also for the pastebin data?
– mrz
Jul 15, 2016 at 13:57
• Sure thing! (minimum character count for comment reached now) Jul 15, 2016 at 14:03

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}

• 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] Jul 15, 2016 at 14:14
• 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 Jul 15, 2016 at 14:17