class Rserve::DataFrame

An R-centric container for storing data frame-ish data

Attributes

colnames[W]

the colnames can be set explicitly, or they will be generated by data.keys

data[RW]

a hash where the keys are the column names and the values are arrays of data

rownames[RW]

rownames as an array of Integers or Strings. nil by default.

Public Class Methods

from_structs(array) click to toggle source

takes an array of structs and returns a data frame object

# 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
new(ordered_hash, rownames=nil) click to toggle source

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)

# File lib/rserve/data_frame.rb, line 40
def initialize(ordered_hash, rownames=nil)
  @data = ordered_hash
  @rownames = rownames
end

Public Instance Methods

==(other) click to toggle source
# File lib/rserve/data_frame.rb, line 45
def ==(other)
  (self.data == other.data) && (self.rownames == other.rownames)
end
colnames() click to toggle source

will use colnames if they’ve been set, otherwise data.keys

# File lib/rserve/data_frame.rb, line 31
def colnames
  @colnames || @data.keys 
end