Module: DaruLite::DataFrame::Setable
- Included in:
- DaruLite::DataFrame
- Defined in:
- lib/daru_lite/data_frame/setable.rb
Instance Method Summary collapse
-
#[]=(*args) ⇒ Object
Insert a new row/vector of the specified name or modify a previous row.
- #add_row(row, index = nil) ⇒ Object
- #add_vector(n, vector) ⇒ Object
- #insert_vector(n, name, source) ⇒ Object
-
#set_at(positions, vector) ⇒ Object
Set vectors by positions.
-
#set_row_at(positions, vector) ⇒ Object
Set rows by positions.
Instance Method Details
#[]=(*args) ⇒ Object
Insert a new row/vector of the specified name or modify a previous row. Instead of using this method directly, use df.row = [1,2,3] to set/create a row ‘:a’ to [1,2,3], or df.vector = [1,2,3] for vectors.
In case a DaruLite::Vector is specified after the equality the sign, the indexes of the vector will be matched against the row/vector indexes of the DataFrame before an insertion is performed. Unmatched indexes will be set to nil.
80 81 82 83 84 85 86 |
# File 'lib/daru_lite/data_frame/setable.rb', line 80 def []=(*args) vector = args.pop axis = extract_axis(args) names = args dispatch_to_axis axis, :insert_or_modify, names, vector end |
#add_row(row, index = nil) ⇒ Object
88 89 90 |
# File 'lib/daru_lite/data_frame/setable.rb', line 88 def add_row(row, index = nil) self.row[*(index || @size)] = row end |
#add_vector(n, vector) ⇒ Object
92 93 94 |
# File 'lib/daru_lite/data_frame/setable.rb', line 92 def add_vector(n, vector) self[n] = vector end |
#insert_vector(n, name, source) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/daru_lite/data_frame/setable.rb', line 96 def insert_vector(n, name, source) raise ArgumentError unless source.is_a? Array vector = DaruLite::Vector.new(source, index: @index, name: @name) @data << vector @vectors = @vectors.add name ordr = @vectors.dup.to_a elmnt = ordr.pop ordr.insert n, elmnt self.order = ordr end |
#set_at(positions, vector) ⇒ Object
Set vectors by positions
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/daru_lite/data_frame/setable.rb', line 53 def set_at(positions, vector) if positions.last == :row positions.pop return set_row_at(positions, vector) end validate_positions(*positions, ncols) vector = if vector.is_a? DaruLite::Vector vector.reindex @index else DaruLite::Vector.new vector end raise SizeError, 'Vector length should match index length' if vector.size != @index.size positions.each { |pos| @data[pos] = vector } end |
#set_row_at(positions, vector) ⇒ Object
Set rows by positions
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/daru_lite/data_frame/setable.rb', line 19 def set_row_at(positions, vector) validate_positions(*positions, nrows) vector = if vector.is_a? DaruLite::Vector vector.reindex @vectors else DaruLite::Vector.new vector end raise SizeError, 'Vector length should match row length' if vector.size != @vectors.size @data.each_with_index do |vec, pos| vec.set_at(positions, vector.at(pos)) end @index = @data[0].index set_size end |