Hi, I''m attempting to create a users statistics controller, and have saved up some questions regarding this for this post. So, please comment on any one of them, if not all :-) 1) Is there a library somewhere for doing stats? (e.g. mean, median, sd, skewness..) on an array in rails? 2) What library would you recommend for "publication ready" (i.e. not cheesy) histograms, scatterplots and barplots? 3) Is there some simple way of doing a frequency table of the values in an array in Ruby? My solution is> m1.each {|mem| out[mem.age] = 0 } > m1.each {|mem| out[mem.age] += 1 }where m1 is a collection of members. Are there better solutions? 4) How would I count the number of occurences in an age range? (i.e. between two values) 5) Converting a Hash to a HTML table seems like a common task. Are there ready-made functions for this? 6) Is there a way of sorting a collection (of members, in my case) by one factor, without accessing the database once for each level of the factor? E.g. "gender" will have either one of two values... So, if i would like to get separate my members by gender (Not very PC, I know, but.. :-) ), do I have to make two different .find calls? Sorry for the number of questions.. but I am trying to get at grip of Rails / Ruby and how to do things The RoR way.. :-) /Fredrik -- "Give up learning, and put an end to your troubles." --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If you are going to do multiple operations on the data (ie., view the same data from a number of different perspectives) you might consider reading it in once and then doing the operations in Ruby. I think that might be what you were hinting at with "not going to the db each time." With that in mind, 3, 4, and 6 can make use of the "group_by" method mixed into Array. This method iterates over the collection and builds a hash in which the keys of the hash are the ''grouping'' and the values are the elements that match that grouping. The ''grouping'' is the result of applying a block. For example: users_by_age = @users.group_by{|user| user.age} Now you can iterate over users_by_age: users_by_age.each_pair do |age, age_collection| puts "There are #{age_collection.size} members who are #{age} years old." end Since you have complete control over the block, you can use the same technique to create bigger bins for your histogram and completely change the grouping mechanism. users_by_gender = @users.group_by{|user| user.gender} this_year = Date.today.year users_by_decade = @users.group_by{|user| (this_year - user.born_on.year)/10} HTH, AndyV On Mar 12, 7:00 am, "Fredrik Karlsson" <dargo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''m attempting to create a users statistics controller, and have saved > up some questions regarding this for this post. > So, please comment on any one of them, if not all :-) > > 1) Is there a library somewhere for doing stats? (e.g. mean, median, > sd, skewness..) on an array in rails? > 2) What library would you recommend for "publication ready" (i.e. not > cheesy) histograms, scatterplots and barplots? > 3) Is there some simple way of doing a frequency table of the values > in an array in Ruby? My solution is > > > m1.each {|mem| out[mem.age] = 0 } > > m1.each {|mem| out[mem.age] += 1 } > > where m1 is a collection of members. Are there better solutions? > > 4) How would I count the number of occurences in an age range? (i.e. > between two values) > 5) Converting a Hash to a HTML table seems like a common task. Are > there ready-made functions for this? > 6) Is there a way of sorting a collection (of members, in my case) by > one factor, without accessing the database once for each level of the > factor? > E.g. "gender" will have either one of two values... So, if i would > like to get separate my members by gender (Not very PC, I know, but.. > :-) ), do I have to make two different .find calls? > > Sorry for the number of questions.. but I am trying to get at grip of > Rails / Ruby and how to do things The RoR way.. :-) > > /Fredrik > > -- > "Give up learning, and put an end to your troubles."--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fantastic! Exactly what I was looking for! Just didn''t have the imagination enough to find the group_by method of arrays to be the solution to almost all my problems. So, what do I plot this created data set with? Any ideas? /Fredrik On Wed, Mar 12, 2008 at 6:05 PM, AndyV <arv7-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote:> > If you are going to do multiple operations on the data (ie., view the > same data from a number of different perspectives) you might consider > reading it in once and then doing the operations in Ruby. I think > that might be what you were hinting at with "not going to the db each > time." > > With that in mind, 3, 4, and 6 can make use of the "group_by" method > mixed into Array. This method iterates over the collection and builds > a hash in which the keys of the hash are the ''grouping'' and the values > are the elements that match that grouping. The ''grouping'' is the > result of applying a block. For example: > > users_by_age = @users.group_by{|user| user.age} > > Now you can iterate over users_by_age: > > users_by_age.each_pair do |age, age_collection| > puts "There are #{age_collection.size} members who are #{age} years > old." > end > > Since you have complete control over the block, you can use the same > technique to create bigger bins for your histogram and completely > change the grouping mechanism. > > users_by_gender = @users.group_by{|user| user.gender} > > this_year = Date.today.year > users_by_decade = @users.group_by{|user| (this_year - > user.born_on.year)/10} > > > HTH, > AndyV > > > > On Mar 12, 7:00 am, "Fredrik Karlsson" <dargo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, > > > > I''m attempting to create a users statistics controller, and have saved > > up some questions regarding this for this post. > > So, please comment on any one of them, if not all :-) > > > > 1) Is there a library somewhere for doing stats? (e.g. mean, median, > > sd, skewness..) on an array in rails? > > 2) What library would you recommend for "publication ready" (i.e. not > > cheesy) histograms, scatterplots and barplots? > > 3) Is there some simple way of doing a frequency table of the values > > in an array in Ruby? My solution is > > > > > m1.each {|mem| out[mem.age] = 0 } > > > m1.each {|mem| out[mem.age] += 1 } > > > > where m1 is a collection of members. Are there better solutions? > > > > 4) How would I count the number of occurences in an age range? (i.e. > > between two values) > > 5) Converting a Hash to a HTML table seems like a common task. Are > > there ready-made functions for this? > > 6) Is there a way of sorting a collection (of members, in my case) by > > one factor, without accessing the database once for each level of the > > factor? > > E.g. "gender" will have either one of two values... So, if i would > > like to get separate my members by gender (Not very PC, I know, but.. > > :-) ), do I have to make two different .find calls? > > > > Sorry for the number of questions.. but I am trying to get at grip of > > Rails / Ruby and how to do things The RoR way.. :-) > > > > /Fredrik > > > > -- > > "Give up learning, and put an end to your troubles." > > >-- "Give up learning, and put an end to your troubles." --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
There are a few more statistics ideas here: http://derrick.pallas.us/ruby-stats/ Pretty clever and simple solutions for many statistics needs. Lots of plotting tools... you may want to look at Tioga (http:// tioga.rubyforge.org/) or rgplot (http://rgplot.rubyforge.org/), a Ruby interface to gnuplot (http://www.gnuplot.info/). --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---