Class: Daru::Core::GroupBy

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, names) ⇒ GroupBy

Returns a new instance of GroupBy.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/daru/core/group_by.rb', line 7

def initialize context, names
  @groups = {}
  @non_group_vectors = context.vectors.to_a - names
  @context = context
  vectors = names.map { |vec| context.vector[vec].to_a }
  tuples  = vectors[0].zip(*vectors[1..-1])
  keys    = tuples.uniq.sort

  keys.each do |key|
    @groups[key] = all_indices_for(tuples, key)
  end
  @groups.freeze
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



5
6
7
# File 'lib/daru/core/group_by.rb', line 5

def groups
  @groups
end

Instance Method Details

#countObject



64
65
66
67
# File 'lib/daru/core/group_by.rb', line 64

def count
  width = @non_group_vectors.size
  Daru::DataFrame.new([size]*width, order: @non_group_vectors)
end

#firstObject



33
34
35
# File 'lib/daru/core/group_by.rb', line 33

def first
  head(1)
end

#get_group(group) ⇒ Object

Returns one of the selected groups as a DataFrame.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/daru/core/group_by.rb', line 86

def get_group group
  indexes   = @groups[group]
  elements  = []

  @context.each_vector do |vector|
    elements << vector.to_a
  end
  rows = []
  transpose = elements.transpose

  indexes.each do |idx|
    rows << transpose[idx]
  end
  Daru::DataFrame.rows(rows, index: @context.index[indexes], order: @context.vectors)
end

#head(quantity = 5) ⇒ Object



41
42
43
# File 'lib/daru/core/group_by.rb', line 41

def head quantity=5
  select_groups_from :first, quantity
end

#lastObject



37
38
39
# File 'lib/daru/core/group_by.rb', line 37

def last
  tail(1)
end

#maxObject

Find the max element of each numeric vector group.



76
77
78
# File 'lib/daru/core/group_by.rb', line 76

def max
  apply_method :numeric, :max
end

#meanObject

Calculate mean of numeric groups, excluding missing values.



50
51
52
# File 'lib/daru/core/group_by.rb', line 50

def mean
  apply_method :numeric, :mean
end

#medianObject

Calculate the median of numeric groups, excluding missing values.



55
56
57
# File 'lib/daru/core/group_by.rb', line 55

def median
  apply_method :numeric, :median
end

#minObject

Find the min element of each numeric vector group.



81
82
83
# File 'lib/daru/core/group_by.rb', line 81

def min
  apply_method :numeric, :min
end

#sizeObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/daru/core/group_by.rb', line 21

def size
  index = 
  if multi_indexed_grouping?
    Daru::MultiIndex.new symbolize(@groups.keys)
  else
    Daru::Index.new symbolize(@groups.keys.flatten)
  end

  values = @groups.values.map { |e| e.size }
  Daru::Vector.new(values, index: index, name: :size)
end

#stdObject

Calculate sample standard deviation of numeric vector groups, excluding missing values.



71
72
73
# File 'lib/daru/core/group_by.rb', line 71

def std
  apply_method :numeric, :std
end

#sumObject

Calculate sum of numeric groups, excluding missing values.



60
61
62
# File 'lib/daru/core/group_by.rb', line 60

def sum
  apply_method :numeric, :sum
end

#tail(quantity = 5) ⇒ Object



45
46
47
# File 'lib/daru/core/group_by.rb', line 45

def tail quantity=5
  select_groups_from :last, quantity
end