Class: Rserve::DataFrame
- Inherits:
-
Object
- Object
- Rserve::DataFrame
- Defined in:
- lib/rserve/data_frame.rb
Overview
An R-centric container for storing data frame-ish data
Instance Attribute Summary collapse
-
#colnames ⇒ Object
will use colnames if they’ve been set, otherwise data.keys.
-
#data ⇒ Object
a hash where the keys are the column names and the values are arrays of data.
-
#rownames ⇒ Object
rownames as an array of Integers or Strings.
Class Method Summary collapse
-
.from_structs(array) ⇒ Object
takes an array of structs and returns a data frame object.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(ordered_hash, rownames = nil) ⇒ DataFrame
constructor
takes an ordered hash, where the col_name is the key and the data rows are an array of values.
Constructor Details
#initialize(ordered_hash, rownames = nil) ⇒ DataFrame
takes an ordered hash, where the col_name is the key and the data rows are an array of values. The default ordering of the hash keys will be used as the colnames. This works great for ruby 1.9 (which remembers ordering). Use an OrderedHash for ruby 1.8. The rownames can be used to specify the names of the rows (remains nil if no values specified)
40 41 42 43 |
# File 'lib/rserve/data_frame.rb', line 40 def initialize(ordered_hash, rownames=nil) @data = ordered_hash @rownames = rownames end |
Instance Attribute Details
#colnames ⇒ Object
will use colnames if they’ve been set, otherwise data.keys
31 32 33 |
# File 'lib/rserve/data_frame.rb', line 31 def colnames @colnames || @data.keys end |
#data ⇒ Object
a hash where the keys are the column names and the values are arrays of data
12 13 14 |
# File 'lib/rserve/data_frame.rb', line 12 def data @data end |
#rownames ⇒ Object
rownames as an array of Integers or Strings. nil by default.
9 10 11 |
# File 'lib/rserve/data_frame.rb', line 9 def rownames @rownames end |
Class Method Details
.from_structs(array) ⇒ Object
takes an array of structs and returns a data frame object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rserve/data_frame.rb', line 15 def self.from_structs(array) names = array.first.members lengthwise_arrays = names.map { Array.new(array.length) } array.each_with_index do |struct,m| struct.values.each_with_index do |val,n| lengthwise_arrays[n][m] = val end end data = {} names.zip(lengthwise_arrays) do |name, lengthwise_array| data[name] = lengthwise_array end self.new(data) end |
Instance Method Details
#==(other) ⇒ Object
45 46 47 |
# File 'lib/rserve/data_frame.rb', line 45 def ==(other) (self.data == other.data) && (self.rownames == other.rownames) end |