Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/easystats.rb
Instance Method Summary collapse
-
#mean ⇒ Object
(also: #average)
take in an array of numbers and calculate the mean (average).
-
#median ⇒ Object
take in the array of numbers and calculate the median.
-
#mode ⇒ Object
take in an array of numbers and return the mode.
-
#range ⇒ Object
take in an array of numbers and calculate the range.
-
#standard_deviation ⇒ Object
take in an array of numbers and calculate the standard deviation.
-
#sum ⇒ Object
take in an array of numbers and calculate the sum.
- #variance ⇒ Object
Instance Method Details
#mean ⇒ Object Also known as: average
take in an array of numbers and calculate the mean (average)
3 4 5 6 7 |
# File 'lib/easystats.rb', line 3 def mean return unless self.any? self.sum / self.count.to_f end |
#median ⇒ Object
take in the array of numbers and calculate the median
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/easystats.rb', line 12 def median return unless self.any? data = self halfway = data.count / 2 # Sort the array data = data.sort # The median will be different based on the number of numbers in the array # If there is an even number in the array if(data.count % 2) == 0 median = (data[halfway] + data[halfway-1]) / 2.0 # Else, there is an odd number of elements in the array else median = data[halfway] end median end |
#mode ⇒ Object
take in an array of numbers and return the mode
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/easystats.rb', line 36 def mode return unless self.any? # Sort the array data = self.sort # create a flag to tell the user if all the numbers only appear once no_mode = true # The variable that will hold the highest number highest_value = 0 # The variable that holds the most time the value appears most_times = 0 # Create a new hash to hold the numbers tmp = Hash.new # Populate the hash data.each do |num| if tmp["#{num}"].nil? == false tmp["#{num}"] = tmp["#{num}"].to_i + 1 else tmp["#{num}"] = 1 end end # Check to make sure that there is a mode data.each do |num| if tmp["#{num}"].to_i > 0 no_mode = false end end if no_mode == true nil else data.each do |num| if tmp["#{num}"].to_i > most_times highest_value = num most_times = tmp["#{num}"] end end # now loop through again just to make sure another number doesn't appear an equal number of times data.each do |num| if num != highest_value if tmp["#{num}"].to_i == most_times no_mode = true end end end if no_mode == true nil else highest_value end end end |
#range ⇒ Object
take in an array of numbers and calculate the range
98 99 100 101 102 103 |
# File 'lib/easystats.rb', line 98 def range return unless self.any? data = self.sort data.last - data.first end |
#standard_deviation ⇒ Object
take in an array of numbers and calculate the standard deviation
106 107 108 109 110 111 |
# File 'lib/easystats.rb', line 106 def standard_deviation return unless self.any? return 0 if self.one? Math::sqrt(self.sum_of_deviations_squared / (self.count-1)) end |
#sum ⇒ Object
take in an array of numbers and calculate the sum
114 115 116 |
# File 'lib/easystats.rb', line 114 def sum self.reduce { |total, number| total + number } end |
#variance ⇒ Object
118 119 120 121 122 |
# File 'lib/easystats.rb', line 118 def variance return unless self.any? self.sum_of_deviations_squared / self.count.to_f end |