Class: Daru::Core::GroupBy
- Inherits:
-
Object
- Object
- Daru::Core::GroupBy
- Defined in:
- lib/daru/core/group_by.rb
Instance Attribute Summary collapse
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
Instance Method Summary collapse
- #count ⇒ Object
- #first ⇒ Object
-
#get_group(group) ⇒ Object
Returns one of the selected groups as a DataFrame.
- #head(quantity = 5) ⇒ Object
-
#initialize(context, names) ⇒ GroupBy
constructor
A new instance of GroupBy.
- #last ⇒ Object
-
#max ⇒ Object
Find the max element of each numeric vector group.
-
#mean ⇒ Object
Calculate mean of numeric groups, excluding missing values.
-
#median ⇒ Object
Calculate the median of numeric groups, excluding missing values.
-
#min ⇒ Object
Find the min element of each numeric vector group.
- #size ⇒ Object
-
#std ⇒ Object
Calculate sample standard deviation of numeric vector groups, excluding missing values.
-
#sum ⇒ Object
Calculate sum of numeric groups, excluding missing values.
- #tail(quantity = 5) ⇒ Object
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
#groups ⇒ Object (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
#count ⇒ Object
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 |
#first ⇒ Object
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 |
#last ⇒ Object
37 38 39 |
# File 'lib/daru/core/group_by.rb', line 37 def last tail(1) end |
#max ⇒ Object
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 |
#mean ⇒ Object
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 |
#median ⇒ Object
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 |
#min ⇒ Object
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 |
#size ⇒ Object
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 |
#std ⇒ Object
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 |
#sum ⇒ Object
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 |