Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/daru/monkeys.rb
Instance Method Summary collapse
- #daru_vector(name = nil, index = nil, dtype = :array) ⇒ Object (also: #dv)
-
#recode_repeated ⇒ Object
Recode repeated values on an array, adding the number of repetition at the end Example: a=%wb c c d d d e a.recode_repeated => [“a”,“b”,“c_1”,“c_2”,“d_1”,“d_2”,“d_3”,“e”].
- #to_index ⇒ Object
Instance Method Details
#daru_vector(name = nil, index = nil, dtype = :array) ⇒ Object Also known as: dv
26 27 28 |
# File 'lib/daru/monkeys.rb', line 26 def daru_vector name=nil, index=nil, dtype=:array Daru::Vector.new self, name: name, index: index, dtype: dtype end |
#recode_repeated ⇒ Object
Recode repeated values on an array, adding the number of repetition at the end Example:
a=%w{a b c c d d d e}
a.recode_repeated
=> ["a","b","c_1","c_2","d_1","d_2","d_3","e"]
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/daru/monkeys.rb', line 8 def recode_repeated return self if size == uniq.size duplicated = group_by { |n| n } .select { |_, g| g.size > 1 }.map(&:first) counter = duplicated.collect { |n| [n, 0] }.to_h collect do |n| if counter.key?(n) counter[n] += 1 '%s_%d' % [n, counter[n]] else n end end end |