Class: Rust::Factor
Class Method Summary
collapse
Instance Method Summary
collapse
pull_priority, #r_hash, #r_mirror, #r_mirror_to
Constructor Details
#initialize(values, levels) ⇒ Factor
Returns a new instance of Factor.
23
24
25
26
|
# File 'lib/rust/core/types/factor.rb', line 23
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
64
65
66
67
68
|
# File 'lib/rust/core/types/factor.rb', line 64
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
5
6
7
|
# File 'lib/rust/core/types/factor.rb', line 5
def self.can_pull?(type, klass)
return klass == "factor"
end
|
.pull_variable(variable, type, klass) ⇒ Object
9
10
11
12
13
14
|
# File 'lib/rust/core/types/factor.rb', line 9
def self.pull_variable(variable, type, klass)
levels = Rust["levels(#{variable})"]
values = Rust["as.integer(#{variable})"]
return Factor.new(values, levels)
end
|
Instance Method Details
#==(other) ⇒ Object
32
33
34
35
36
|
# File 'lib/rust/core/types/factor.rb', line 32
def ==(other)
return false unless other.is_a?(Factor)
return @levels == other.levels && self.to_a == other.to_a
end
|
38
39
40
|
# File 'lib/rust/core/types/factor.rb', line 38
def [](i)
FactorValue.new(@values[i], @levels[@values[i] - 1])
end
|
#[]=(i, value) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/rust/core/types/factor.rb', line 42
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
|
74
75
76
|
# File 'lib/rust/core/types/factor.rb', line 74
def inspect
self.to_a.inspect
end
|
28
29
30
|
# File 'lib/rust/core/types/factor.rb', line 28
def levels
@levels
end
|
#load_in_r_as(variable_name) ⇒ Object
16
17
18
19
20
21
|
# File 'lib/rust/core/types/factor.rb', line 16
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
|
60
61
62
|
# File 'lib/rust/core/types/factor.rb', line 60
def to_a
@values.map { |v| FactorValue.new(v, @levels[v - 1]) }
end
|
70
71
72
|
# File 'lib/rust/core/types/factor.rb', line 70
def to_s
self.to_a.to_s
end
|