We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Frequency count for values in a column
Page Index Toggle Pages: 1
Frequency count for values in a column? (Read 572 times)
Frequency count for values in a column?
Jan 29th, 2010, 12:05am
 
Hi All,

I'm very new to Processing and am reading Fry's book Vizualizing Data, have finished reading until chap 4 and its been great so far. I'm applying it to a project and got into a difficulty recently..

In my tsv file, I have a column of data (student names) and I need to determine how many different unique values there are in that column (how many different students), and also, what is the frequency of each type of value (how many repetitions for each student name), preferrably get an output in an array format.

i.e. From:

Andy
Julie
Andy
Brad
Tom
Brad

To:

names [Andy, Julie, Brad, Tom]
freq     [     2,      1,      2,      1]

Does anyone know if the Table class featured in the book has any methods to accomplish this?

Or, if not, can anyone share if this is possible and how can it be done? Or is it explained somewhere in the later chapters of the book and I just need to read those chapters first? Smiley

Many thanks in advance!

Dave
Re: Frequency count for values in a column?
Reply #1 - Jan 29th, 2010, 2:24am
 
Code:
String[] students =
{
"Andy",
"Julie",
"Andy",
"Brad",
"Tom",
"Brad"
};

HashMap stats = new HashMap();
for (int i = 0; i < students.length; i++)
{
String student = students[i];
Object o = stats.get(student);
int nb = 0;
if (o != null)
{
nb = (Integer) o;
}
stats.put(student, ++nb);
}
println(stats);
Page Index Toggle Pages: 1