3
$\begingroup$

I want to create a "value-frequency" table from the following expression.

Counts[Sort[RandomInteger[{1, 5}, 500]]] // TableForm

The generated table should be in LaTeX form either

  • two rows and n columns. first row: Value, second row: Frequency
  • n rows and 2 columns. first column: Value, second column: Frequency

Optionally with headings "Value" and "Frequency" to make it look nice.

How to achieve this?

$\endgroup$
4
$\begingroup$
SeedRandom[1]
counts = Counts[Sort[RandomInteger[{1, 5}, 500]]];
TeXForm @ Grid[Join[{{"Value", "Frequency"}}, KeyValueMap[List][counts]], Dividers -> All]

$\begin{array}{|c|c|} \hline \text{Value} & \text{Frequency} \\ \hline 1 & 104 \\ \hline 2 & 92 \\ \hline 3 & 97 \\ \hline 4 & 101 \\ \hline 5 & 106 \\ \hline \end{array}$

TeXForm@Grid[Transpose@Join[{{"Value", "Frequency"}}, KeyValueMap[List][counts]],
   Dividers -> All]

$\begin{array}{|c|c|c|c|c|c|} \hline \text{Value} & 1 & 2 & 3 & 4 & 5 \\ \hline \text{Frequency} & 104 & 92 & 97 & 101 & 106 \\ \hline \end{array}$

$\endgroup$
3
$\begingroup$

One way to make your table:

TeXForm@TableForm[Sort[Tally@RandomInteger[{1, 5}, 500]], 
        TableHeadings -> {None, {"Value", "Frequency"}}]

The output is the Latex code for:

$\begin{array}{cc} \text{Value} & \text{Frequency} \\ 1 & 89 \\ 2 & 94 \\ 3 & 117 \\ 4 & 93 \\ 5 & 107 \\ \end{array}$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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