Class: Array

Inherits:
Object show all
Defined in:
lib/overwrite.rb

Instance Method Summary collapse

Instance Method Details

#collect_with_indexObject

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_RArray

Convert array values for R

Returns:



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

#meanNumeric

Get the mean of an array

Returns:



132
133
134
# File 'lib/overwrite.rb', line 132

def mean
  self.compact.inject{ |sum, el| sum + el }.to_f / self.compact.size
end

#medianNumeric

Get the median of an array

Returns:



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_varianceNumeric

Get the variance of an array

Returns:



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_deviationNumeric

Get the standard deviation of an array

Returns:



146
147
148
# File 'lib/overwrite.rb', line 146

def standard_deviation
  Math.sqrt(self.sample_variance)
end

#sum_sizeInteger

Sum the size of single arrays in an array of arrays

Parameters:

  • Array (Array)

    of arrays

Returns:

  • (Integer)

    Sum of size of array elements



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.

Parameters:

  • Array (Array)

    to test.

Returns:

  • (TrueClass, FalseClass)


118
119
120
# File 'lib/overwrite.rb', line 118

def zero_variance?
  return self.uniq.size == 1
end