I want to know how to make a tally board in a specific data appear in a list. E.G.
n ={1,2,1,4,3,1,4,5,3,2,4,5,6,3,4,2,2,5,2}
and get
Case 1 2 3 4 5 6 n
Frequency 3 5 3 4 3 1 19
Hoping for an answer thanks a lot!
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI want to know how to make a tally board in a specific data appear in a list. E.G.
n ={1,2,1,4,3,1,4,5,3,2,4,5,6,3,4,2,2,5,2}
and get
Case 1 2 3 4 5 6 n
Frequency 3 5 3 4 3 1 19
Hoping for an answer thanks a lot!
Your data:
n ={1,2,1,4,3,1,4,5,3,2,4,5,6,3,4,2,2,5,2};
To get the Tally
list:
Tally[n]
To format it into a Tally Table we use TableForm
:
Block[{t = Tally[n]},
TableForm[t, TableHeadings -> {None, {"Number", "Frequency"}}]
]
It happens now that its not sorted, nor in the orientation you wanted so we use SortBy
, First
and Transpose
. Also include the total number of cases at the end
Block[
{tally, table},
tally = SortBy[Tally[n], First];
table = Transpose@Append[tally, {"n", Length@n}];
TableForm[table, TableHeadings -> {{"Case", "Frequency"}, None}]]
Which is the format the OP requested.
There is also ... EmpiricalDistribution
:
lst = {1, 2, 1, 4, 3, 1, 4, 5, 3, 2, 4, 5, 6, 3, 4, 2, 2, 5, 2};
dist = EmpiricalDistribution[lst];
tally = {#["Domain"], #["SampleSize"] #["Weights"]} &[dist];
Transpose[tally] == Sort@Tally[lst]
(* True *)
TableForm[tally, TableHeadings -> {{"values", "frequency"}, None}]
tally2 = {Join @@ {#["Domain"], {"nobs"}},
Join @@ {#["SampleSize"] #["Weights"], {#["SampleSize"]}}} &[dist];
TableForm[tally2, TableHeadings -> {{"values", "frequency"}, None},
TableAlignments -> Center]
Tally
is the function you need.
n = {1, 2, 1, 4, 3, 1, 4, 5, 3, 2, 4, 5, 6, 3, 4, 2, 2, 5, 2};
Tally[n]
produces:
{{1, 3}, {2, 5}, {4, 4}, {3, 3}, {5, 3}, {6, 1}}
You can use Count
for a specific element. Other ways to tally:
{#,Count[n, #]} & /@ Union[n]
Or
Last@Reap[Sow[1, #] & /@ n, _, {#1, Total@#2} &]
CountsBy[n, Identity] // Dataset
Update thanks to RunnyKines comment:
Dataset @ Counts @ n
does the same job more elegantly
Counts
instead i.e. Dataset @ Counts @ n
$\endgroup$
Sep 20, 2014 at 9:09
Tally
but also sorting,TableHeadings
, calculateLength
of the dataset and probablyTableForm
or similar. There is significant overlap, but its not an exacte duplicate $\endgroup$