Sam. G - 1 month ago 6
Ruby Question

# Array of hashes problems

If I have the following data

``````First Name =>, Last Name, Age, Income, Household Size, Gender, Education
Jon, Smith, 25, 50000, 1, Male, College
Jane, Davies, 30, 60000, 3, Female, High School
Sam, Farelly, 32, 80000, 2, Unspecified, College
Joan, Favreau, 35, 65000, 4, Female, College
Sam, McNulty, 38, 63000, 3, Male, College
Mark, Minahan, 48, 78000, 5, Male, High School
Susan, Umani, 45, 75000, 2, Female, College
Bill, Perault, 24, 45000, 1, Male, Did Not Complete High School
Doug, Stamper, 45, 75000, 1, Male, College
Francis, Underwood, 52, 100000, 2, Male, College
``````

and I want to create an array of hashes to answer the following questions
Average age
Average income
Average household size
Female Percentage
Male Percentage
Unspecified Gender Percentage
Percent of those who obtained a college education level Percent of those who obtained a high school education level
Percent of those that did not finish high school

Would I be able to organize the data in this way

``````voter_demographics = [
{
:firstname => ["Jon", "Jane", "Sam", "Joan", "Sam", "Mark", "Susan", "Bill", "Doug", "Francis"],
:lastname => ["Smith", "Davies", "Farelly", "Favreau", "McNulty", "Minahan", "Umani", "Perault", "Stamper", "Underwood"],
:age => [25, 30, 32, 35, 38, 48, 45, 24, 45, 52],
:income => [50000, 60000, 80000, 65000, 63000, 78000, 75000, 45000, 75000, 100000],
:household_size => [1, 3, 2, 4, 3, 5, 2, 1, 1, 2],
:gender => ["male", "female", "unspecified", "female", "male", "male", "female", "male", "male", "male"],
:education => ["college", "high school", "college", "college", "college", "high school", "college", "did not complete high school", "college", "college"]
}
]
``````

If someone could just help me get started on question one(Average age ). Im still struggling on hashes and how to call out each data.

``````For the first question ; Find Average age. Would the following work.
sum = 0
voter_demographics.each do |:age|
sum = sum + :age
average = sum / :age.length
puts "The average is #{average}".
``````

Im stuck on this.
And if there are any resources for beginners you could recommend on hashes and array of hashes that would be great!

Answer

I'm wondering why you have `voter_demographics` as an array. If you remove the square brackets then it would just be a hash, like this:

``````voter_demographics = {
:firstname => ["Jon", "Jane", "Sam", "Joan", "Sam", "Mark", "Susan", "Bill", "Doug", "Francis"],
:lastname => ["Smith", "Davies", "Farelly", "Favreau", "McNulty", "Minahan", "Umani", "Perault", "Stamper", "Underwood"],
:age => [25, 30, 32, 35, 38, 48, 45, 24, 45, 52],
:income => [50000, 60000, 80000, 65000, 63000, 78000, 75000, 45000, 75000, 100000],
:household_size => [1, 3, 2, 4, 3, 5, 2, 1, 1, 2],
:gender => ["male", "female", "unspecified", "female", "male", "male", "female", "male", "male", "male"],
:education => ["college", "high school", "college", "college", "college", "high school", "college", "did not complete high school", "college", "college"]
}
``````

Then you can access values in the hash like this:

``````voter_demographics[:age]
``````

And finding your average would work like this:

``````voter_demographics[:age].inject(:+).to_f / voter_demographics[:age].size
``````

or

``````ages = voter_demographics[:age]
ages.inject(:+).to_f / ages.size
``````

The `.to_f` assumes you want a float number back, so my solutions would give you 37.4 instead of 37

Another solution would be to just use regular variables instead of a hash:

``````firstnames = ["Jon", "Jane", "Sam", "Joan", "Sam", "Mark", "Susan", "Bill", "Doug", "Francis"],
lastnames = ["Smith", "Davies", "Farelly", "Favreau", "McNulty", "Minahan", "Umani", "Perault", "Stamper", "Underwood"],
ages = [25, 30, 32, 35, 38, 48, 45, 24, 45, 52],
incomes = [50000, 60000, 80000, 65000, 63000, 78000, 75000, 45000, 75000, 100000],
household_sizes = [1, 3, 2, 4, 3, 5, 2, 1, 1, 2],
genders = ["male", "female", "unspecified", "female", "male", "male", "female", "male", "male", "male"],
educations = ["college", "high school", "college", "college", "college", "high school", "college", "did not complete high school", "college", "college"]
``````

Then this would work:

``````ages.inject(:+).to_f / ages.size
``````
Source (Stackoverflow)
Comments