Class: Simpler::DataFrame
- Inherits:
-
Object
- Object
- Simpler::DataFrame
- Defined in:
- lib/simpler/data_frame.rb
Overview
This is an R-centric container for storing data
Instance Attribute Summary collapse
-
#col_names ⇒ Object
this is necessary for.
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#row_names ⇒ Object
Returns the value of attribute row_names.
Instance Method Summary collapse
-
#initialize(hash, row_names = nil, col_names = nil) ⇒ DataFrame
constructor
takes a hash, where the col_name is the key and the data rows are an array of values.
-
#to_r(usefile = nil) ⇒ Object
creates the code to transform the object into R code.
Constructor Details
#initialize(hash, row_names = nil, col_names = nil) ⇒ DataFrame
takes a 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, unless overridden with col_names. The row_names can be used to specify the names of the rows (remains nil if no values specified)
13 14 15 16 17 |
# File 'lib/simpler/data_frame.rb', line 13 def initialize(hash, row_names=nil, col_names=nil) @hash = hash @row_names = row_names @col_names = col_names || @hash.keys end |
Instance Attribute Details
#col_names ⇒ Object
this is necessary for
6 7 8 |
# File 'lib/simpler/data_frame.rb', line 6 def col_names @col_names end |
#hash ⇒ Object
Returns the value of attribute hash.
8 9 10 |
# File 'lib/simpler/data_frame.rb', line 8 def hash @hash end |
#row_names ⇒ Object
Returns the value of attribute row_names.
7 8 9 |
# File 'lib/simpler/data_frame.rb', line 7 def row_names @row_names end |
Instance Method Details
#to_r(usefile = nil) ⇒ Object
creates the code to transform the object into R code. If usefile is specified, the dataframe is written as a table and the code generated will read in the table as a data frame.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/simpler/data_frame.rb', line 22 def to_r(usefile=nil) if usefile raise NotImplementedError, "not implemented just yet" else # build the vectors lines = @col_names.map do |name| val = @hash[name] "#{Simpler.varname(val)} <- c(#{val.join(',')})" end args = @col_names.map do |name| "#{name}=#{Simpler.varname(@hash[name])}" end if @row_names varname = Simpler.varname(@row_names) lines << "#{varname} <- c(#{@row_names.map {|v| "\"#{v}\""}.join(',')})" args.push("row.names=#{varname}") end lines << "#{Simpler.varname(self)} <- data.frame(#{args.join(', ')})" lines.join("\n") << "\n" end end |