Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/daru/monkeys.rb

Instance Method Summary collapse

Instance Method Details

#daru_vector(name = nil, index = nil, dtype = :array) ⇒ Object Also known as: dv



38
39
40
# File 'lib/daru/monkeys.rb', line 38

def daru_vector name=nil, index=nil, dtype=:array
  Daru::Vector.new self, name: name, index: index, dtype: dtype
end

#recode_repeatedObject

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
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/daru/monkeys.rb', line 8

def recode_repeated
  if size != uniq.size
    # Find repeated
    repeated = inject({}) do |acc, v|
      if acc[v].nil?
        acc[v] = 1
      else
        acc[v] += 1
      end
      acc
    end.select { |_k, v| v > 1 }.keys

    ns = repeated.inject({}) do |acc, v|
      acc[v] = 0
      acc
    end

    collect do |f|
      if repeated.include? f
        ns[f] += 1
        sprintf('%s_%d', f, ns[f])
      else
        f
      end
    end
  else
    self
  end
end

#to_indexObject



44
45
46
# File 'lib/daru/monkeys.rb', line 44

def to_index
  Daru::Index.new self
end