Stem-and-Leaf Diagram

Author

Eric W. Weisstein
March 1, 2009

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Statistics/Stem-and-LeafDiagram.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/Stem-and-LeafDiagram.html.

©2009 Wolfram Research, Inc. except for portions noted otherwise

Initialization

<<StatisticalPlots`

Quit

Additional Authors

Code

StemAndLeaf[data_List?VectorQ]:=Module[{d=IntegerDigits/@Sort[data,Less]},
{FromDigits[Drop[First[#],-1]],Last/@#}&/@
Split[d,Equal[Drop[#1,-1],Drop[#2,-1]]&]
]

StemAndLeaf2[data_]:=StemAndLeaf2[data,{2,1}]

StemAndLeaf2[data_,{leftdigits_,rightdigits_}]:=Module[{totaldigs=leftdigits+rightdigits,maxexponent=Floor[Log[10,Max[data]]],digs},digs=Map[First[RealDigits[#,10,totaldigs,maxexponent]]&,data];
digs=Map[{FromDigits[Take[#,leftdigits]],FromDigits[Drop[#,leftdigits]]}&,digs];
digs=Split[Sort[digs],First[#1]===First[#2]&];
Map[{#[[1,1]],Sort[#[[All,2]]]}&,digs]
]

StemAndLeafForm[digs_]:=DisplayForm[GridBox[{First[#],RowBox[Last[#]]}&/@digs,ColumnLines->{1,0},ColumnAlignments->{Right,Left}]]

Examples

data=Table[Random[Integer,{90,150}],{20}]

data={147,117,101,149,145,105,93,94,114,104,136,140,121,145,120,142,98,135,135,132};

Sort[data]

Eric's

StemAndLeaf[data]//ColumnForm

John's

StemAndLeaf2[data]//ColumnForm

StemAndLeaf2[data]//StemAndLeafForm

Built-In

g1=StemLeafPlot[{147,117,101,149,145,105,93,94,114,104,136,140,121,145,120,142,98,135,135,132},StemExponent->1]

g2=StemLeafPlot[{147,117,101,149,145,105,93,94},{114,104,136,140,121,145,120,142,98,135,135,132},StemExponent->1]

Grid[{{g1,g2}},Spacings->5]