Class: Array
Instance Method Summary collapse
-
#collect_with_index ⇒ Object
Collect array with index in analogy to each_with_index.
-
#for_R ⇒ Array
Convert array values for R.
-
#mean ⇒ Numeric
Get the mean of an array.
-
#median ⇒ Numeric
Get the median of an array.
-
#sample_variance ⇒ Numeric
Get the variance of an array.
-
#standard_deviation ⇒ Numeric
Get the standard deviation of an array.
-
#sum_size ⇒ Integer
Sum the size of single arrays in an array of arrays.
-
#zero_variance? ⇒ TrueClass, FalseClass
Check if the array has just one unique value.
Instance Method Details
#collect_with_index ⇒ Object
Collect array with index
in analogy to each_with_index
163 164 165 166 167 168 169 |
# File 'lib/overwrite.rb', line 163 def collect_with_index result = [] self.each_with_index do |elt, idx| result << yield(elt, idx) end result end |
#for_R ⇒ Array
Convert array values for R
152 153 154 155 156 157 158 159 |
# File 'lib/overwrite.rb', line 152 def for_R if self.first.is_a?(String) #"\"#{self.collect{|v| v.sub('[','').sub(']','')}.join(" ")}\"" # quote and remove square brackets "NA" else self.median end end |
#mean ⇒ Numeric
Get the mean of an array
132 133 134 |
# File 'lib/overwrite.rb', line 132 def mean self.compact.inject{ |sum, el| sum + el }.to_f / self.compact.size end |
#median ⇒ Numeric
Get the median of an array
124 125 126 127 128 |
# File 'lib/overwrite.rb', line 124 def median sorted = self.sort len = sorted.length (sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0 end |
#sample_variance ⇒ Numeric
Get the variance of an array
138 139 140 141 142 |
# File 'lib/overwrite.rb', line 138 def sample_variance m = self.mean sum = self.compact.inject(0){|accum, i| accum +(i-m)**2 } sum/(self.compact.length - 1).to_f end |
#standard_deviation ⇒ Numeric
Get the standard deviation of an array
146 147 148 |
# File 'lib/overwrite.rb', line 146 def standard_deviation Math.sqrt(self.sample_variance) end |
#sum_size ⇒ Integer
Sum the size of single arrays in an array of arrays
105 106 107 108 109 110 111 112 113 |
# File 'lib/overwrite.rb', line 105 def sum_size self.inject(0) { |s,a| if a.respond_to?('size') s+=a.size else internal_server_error "No size available: #{a.inspect}" end } end |
#zero_variance? ⇒ TrueClass, FalseClass
Check if the array has just one unique value.
118 119 120 |
# File 'lib/overwrite.rb', line 118 def zero_variance? return self.uniq.size == 1 end |