Justin said:
I've been looking around to learn about histograms, and haven't had any
luck... can someone tell me how a histogram would work for something
like this:
Name, Age
Bob, 2
Jim, 2
Eric, 4
Rob, 5
How would I use a histogram to get a result like this:
Age | Names
2 | Bob, Jim
4 | Eric
5 | Rob
Thanks in advance!!!
Assuming that you have a file that looks exactly like the one above,
this program does exactly what you asked.
# Reads in the datafile, and drops the 'Name, Age' line
data = IO.readlines("nameofdatafile")
data.shift
# Store the data in a hash
hash = Hash.new
data.each {|entry|
name = entry.split(",")[0]
age = entry.split(",")[1].to_i
hash[age] = [] unless hash.has_key?(age) # make sure there's a hash
key
hash[age] << name
}
# Pretty print it for each entry in the hash
puts "Age | Names"
hash.keys.each {|age|
printf("%3i | %s\n",age,hash[age].sort.join(", "))
}
For how histograms work: the general idea is that you create a number of
categories, and assign every single datapoint to its corresponding
category. One way to do this in ruby is to create a hash, in which each
key of the hash is a category, and the corresponding value of the hash
is an empty array. Then you proceed to fill these empty arrays with your
data, by distributing every datapoint to its corresponding array.
(Hopefully this is a general enough answer, it is in essence the same as
Rob's answer.)