3
$\begingroup$

Consider the following:

lists = Import["G:/Notebooks/Lists.csv"];

col7 = lists [[All, 7]];

For[i=0,i<101,i++,Print[Count[col7,i]]]

This will get a 100 cells printed out but what I really want to do is capture this output to another variable as a list. So far the best way I have figured out is to copy the output, edit the text, and manually paste it back in to a variable. There surely has to be a better way?

$\endgroup$
6
  • $\begingroup$ Is this a duplicate of mathematica.stackexchange.com/questions/2930/… , asked just an hour or so ago? $\endgroup$
    – Szabolcs
    Mar 14, 2012 at 15:24
  • 1
    $\begingroup$ It has some similarity. In one example, AppendTo was used. I have tried AppendTo, and it doesn't work. I am also not dealing with a table, just want a one dimensional list of data. I have tried about 5 different ways I could think of to accomplish this using the list manipulation commands and I cannot seem to coax mma to do this. $\endgroup$
    – Sinistar
    Mar 14, 2012 at 15:41
  • $\begingroup$ I really don't understand why Table doesn't work for you, but maybe I'm just too tired. Can you explain? $\endgroup$
    – Szabolcs
    Mar 14, 2012 at 15:44
  • $\begingroup$ @Sinistar: how does the AppendTo not work? Did you define the variable (such as col7 = {}) before using it in AppendTo? Note that AppendTo[col7,someList] is the same as col7 = Append[col7,someList] but both require col7 in this case to be defined $\endgroup$
    – tkott
    Mar 14, 2012 at 17:21
  • $\begingroup$ @tkott It didn't work because you used the wrong brackets. I've edited your answer in the other post. $\endgroup$ Mar 14, 2012 at 18:26

2 Answers 2

5
$\begingroup$

Generally, the solution to this kind of problems is using Table:

Table[Count[col7, i], {i, 100}]

However, in this case I suggest Tally or BinCounts:

Tally[col7]

BinCounts[col7, {0, 100, 1}]

Tally will not list elements that don't appear at all.

$\endgroup$
1
  • $\begingroup$ Seems to be effective... $\endgroup$
    – Sinistar
    Mar 14, 2012 at 15:53
4
$\begingroup$

In addition Array and Map work. Thanks Mr.Wizard. There are some subtle differences in their arguments the documentation does a good job of explaining them.

I assume you were looking for counts of the occurrences of values 0 - 100 in col7.

min = 0;
max = 9; (* In your example it would be 100 *)
col7 = RandomInteger[{min, max}, max*10];

a = {}; For[i = min, i <= max, i++, AppendTo[a, Count[col7, i]]]; a

Array[Count[col7, #] &, max + 1 - min, min]

Map[Count[col7, #] &, Range[min, max]]

Table[Count[col7, i], {i, min, max}]

Sort[Tally[col7], #1[[1]] < #2[[1]] &][[All, 2]]

BinCounts[col7, {min, max + 1, 1}]

Results:

{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}
{6, 11, 14, 6, 11, 9, 7, 10, 7, 9}

The numbers will vary because of how I generated col7, but they should be the same for all six examples.

Performance results:

min = 0;
max = 100;
col7 = RandomInteger[{min, max}, 10000];

enter image description here

$\endgroup$
1
  • $\begingroup$ Nice comparison. The custom sort #1[[1]] < #2[[1]] & is not needed (and slower) because the default sort function will produce the same result in this case. Also, it is faster to use SortBy to sort by the first element, if you need to do that explicitly: SortBy[. . ., First] $\endgroup$
    – Mr.Wizard
    Mar 15, 2012 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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