Class: Rust::Factor
- Inherits:
-
RustDatatype
- Object
- RustDatatype
- Rust::Factor
- Defined in:
- lib/rust/core/types/factor.rb
Overview
Mirror of the factor type in R.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](i) ⇒ Object
Returns the value of the
i
-th element in the factor. -
#[]=(i, value) ⇒ Object
Sets the
value
of thei
-th element in the factor. -
#initialize(values, levels) ⇒ Factor
constructor
Creates a new factor given an array of numeric
values
and symboliclevels
. - #inspect ⇒ Object
-
#levels ⇒ Object
Returns the levels of the factor.
- #load_in_r_as(variable_name) ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
-
#to_a ⇒ Object
Returns an array of FactorValue for the values in this factor.
- #to_s ⇒ Object
Methods inherited from RustDatatype
pull_priority, #r_hash, #r_mirror, #r_mirror_to
Constructor Details
#initialize(values, levels) ⇒ Factor
Creates a new factor given an array of numeric values
and symbolic levels
.
30 31 32 33 |
# File 'lib/rust/core/types/factor.rb', line 30 def initialize(values, levels) @levels = levels.map { |v| v.to_sym } @values = values end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
84 85 86 87 88 |
# File 'lib/rust/core/types/factor.rb', line 84 def method_missing(method, *args, &block) raise NoMethodError, "Undefined method #{method} for Factor" if method.to_s.end_with?("!") || method.end_with?("=") self.to_a.method(method).call(*args, &block) end |
Class Method Details
.can_pull?(type, klass) ⇒ Boolean
9 10 11 |
# File 'lib/rust/core/types/factor.rb', line 9 def self.can_pull?(type, klass) return klass == "factor" end |
Instance Method Details
#==(other) ⇒ Object
42 43 44 45 46 |
# File 'lib/rust/core/types/factor.rb', line 42 def ==(other) return false unless other.is_a?(Factor) return @levels == other.levels && self.to_a == other.to_a end |
#[](i) ⇒ Object
Returns the value of the i
-th element in the factor.
51 52 53 |
# File 'lib/rust/core/types/factor.rb', line 51 def [](i) FactorValue.new(@values[i], @levels[@values[i] - 1]) end |
#[]=(i, value) ⇒ Object
Sets the value
of the i
-th element in the factor. If it is an Integer, the value
must be between 1 and the number of levels of the factor. value
can be either a FactorValue or a String/Symbol.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rust/core/types/factor.rb', line 59 def []=(i, value) raise "The given value is outside the factor bounds" if value.is_a?(Integer) && (value < 1 || value > @levels.size) if value.is_a?(FactorValue) raise "Incompatible factor value, different levels used" unless @levels.include?(value.level) || @levels.index(value.level) + 1 == @value.value value = value.value end if value.is_a?(String) || value.is_a?(Symbol) value = value.to_sym raise "Unsupported value #{value}; expected #{@levels.join(", ")}" unless @levels.include?(value) value = @levels.index(value) + 1 end @values[i] = value end |
#inspect ⇒ Object
94 95 96 |
# File 'lib/rust/core/types/factor.rb', line 94 def inspect self.to_a.inspect end |
#levels ⇒ Object
Returns the levels of the factor.
38 39 40 |
# File 'lib/rust/core/types/factor.rb', line 38 def levels @levels end |
#load_in_r_as(variable_name) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/rust/core/types/factor.rb', line 20 def load_in_r_as(variable_name) Rust['tmp.levels'] = @levels.map { |v| v.to_s } Rust['tmp.values'] = @values Rust._eval("#{variable_name} <- factor(tmp.values, labels=tmp.levels)") end |
#to_a ⇒ Object
Returns an array of FactorValue for the values in this factor.
80 81 82 |
# File 'lib/rust/core/types/factor.rb', line 80 def to_a @values.map { |v| FactorValue.new(v, @levels[v - 1]) } end |
#to_s ⇒ Object
90 91 92 |
# File 'lib/rust/core/types/factor.rb', line 90 def to_s self.to_a.to_s end |