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
157 158 159 160 161 162 163 |
# File 'lib/overwrite.rb', line 157 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
146 147 148 149 150 151 152 153 |
# File 'lib/overwrite.rb', line 146 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
126 127 128 |
# File 'lib/overwrite.rb', line 126 def mean self.compact.inject{ |sum, el| sum + el }.to_f / self.compact.size end |
#median ⇒ Numeric
Get the median of an array
118 119 120 121 122 |
# File 'lib/overwrite.rb', line 118 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
132 133 134 135 136 |
# File 'lib/overwrite.rb', line 132 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
140 141 142 |
# File 'lib/overwrite.rb', line 140 def standard_deviation Math.sqrt(self.sample_variance) end |
#sum_size ⇒ Integer
Sum the size of single arrays in an array of arrays
99 100 101 102 103 104 105 106 107 |
# File 'lib/overwrite.rb', line 99 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.
112 113 114 |
# File 'lib/overwrite.rb', line 112 def zero_variance? return self.uniq.size == 1 end |