5
$\begingroup$

I have a list of percentages as follows:

percent = 
  {21.15, 42.3, 57.68, 73.06, 84.6, 90.37, 96.14, 99.99, 99.99, 99.99, 99.99}

I want to classify these percentages into categories A, B and C. When a value is in the range 0% - 85%, it is A; when it in the range 85% - 95%, it is B; when it in the range 95% - 100%, it is C.

For example: elements 1, 2, 3 and 4 of percent should all be classified into the A category.

How might I produce a TableForm showing all the elements in percent displayed according to category?

$\endgroup$
2

2 Answers 2

9
$\begingroup$
percent = 
  {21.15, 42.3, 57.68, 73.06, 84.6, 90.37, 96.14, 99.99, 99.99, 99.99, 99.99};

{{"A", "B", "C"}, 
  GatherBy[percent, {# <= 85, # <= 95, # <= 100} &]} // TableForm

enter image description here

This can also be written as

{{"A", "B", "C"}, 
  GatherBy[percent, Thread[# <= {85, 95, 100}] &]} // TableForm
$\endgroup$
1
  • $\begingroup$ Elegant solution! $\endgroup$
    – Cassini
    Aug 2, 2015 at 14:29
8
$\begingroup$

Depending on how you want the table aligned, you could use

percent = 
  {21.15, 42.3, 57.68, 73.06, 84.6, 90.37, 96.14, 99.99, 99.99, 99.99, 99.99};

TableForm[BinLists[percent, {{0., 85., 95., 100.}}], 
  TableHeadings -> {{"A", "B", "C"}}]

hz

or

TableForm[{{"A", "B", "C"}, BinLists[percent, {{0., 85., 95., 100.}}]},
  TableAlignments -> {Center, Top}]

vt

$\endgroup$

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.