Class: Rserve::RFactor
- Inherits:
-
Object
- Object
- Rserve::RFactor
- Defined in:
- lib/rserve/rfactor.rb
Instance Attribute Summary collapse
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#index_base ⇒ Object
readonly
Returns the value of attribute index_base.
-
#levels ⇒ Object
readonly
Returns the value of attribute levels.
Instance Method Summary collapse
-
#[](i) ⇒ Object
(also: #at)
returns the level of a given case * @param i case number * @return name.
- #as_integers ⇒ Object
- #as_strings ⇒ Object
- #contains?(li) ⇒ Boolean
- #count(li) ⇒ Object
-
#counts_hash ⇒ Object
return a hash with levels as keys and counts as values.
- #index_at(i) ⇒ Object
-
#initialize(i, v, copy, index_base) ⇒ RFactor
constructor
A new instance of RFactor.
-
#level_index(name) ⇒ Object
return the index of a given level name or -1 if it doesn’t exist.
- #size ⇒ Object
Constructor Details
#initialize(i, v, copy, index_base) ⇒ RFactor
Returns a new instance of RFactor.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rserve/rfactor.rb', line 6 def initialize(i,v,copy,index_base) i=[] if i.nil? v=[] if v.nil? if (copy) @ids=i.dup @levels=v.dup else @ids=i @levels=v end @index_base=index_base end |
Instance Attribute Details
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
3 4 5 |
# File 'lib/rserve/rfactor.rb', line 3 def ids @ids end |
#index_base ⇒ Object (readonly)
Returns the value of attribute index_base.
5 6 7 |
# File 'lib/rserve/rfactor.rb', line 5 def index_base @index_base end |
#levels ⇒ Object (readonly)
Returns the value of attribute levels.
4 5 6 |
# File 'lib/rserve/rfactor.rb', line 4 def levels @levels end |
Instance Method Details
#[](i) ⇒ Object Also known as: at
returns the level of a given case
-
@param i case number
-
@return name. may throw exception if out of range
21 22 23 24 |
# File 'lib/rserve/rfactor.rb', line 21 def [](i) li = @ids[i]-index_base return (li<0 or li>levels.length) ? nil : levels[li] end |
#as_integers ⇒ Object
48 49 50 |
# File 'lib/rserve/rfactor.rb', line 48 def as_integers @ids end |
#as_strings ⇒ Object
51 52 53 |
# File 'lib/rserve/rfactor.rb', line 51 def as_strings @ids.map {|v| v==REXP::Integer::NA ? nil : @levels[v-index_base]} end |
#contains?(li) ⇒ Boolean
25 26 27 28 |
# File 'lib/rserve/rfactor.rb', line 25 def contains?(li) li=level_index(li) if li.is_a? String @ids.any? {|v| v==li} end |
#count(li) ⇒ Object
38 39 40 41 |
# File 'lib/rserve/rfactor.rb', line 38 def count(li) li=level_index(li) if li.is_a? String @ids.inject(0) {|ac,v| ac+ ((v==li) ? 1 : 0 ) } end |
#counts_hash ⇒ Object
return a hash with levels as keys and counts as values
43 44 45 46 47 |
# File 'lib/rserve/rfactor.rb', line 43 def counts_hash h=@levels.inject({}) {|ac,v| ac[v]=0;ac} @ids.each {|v| h[@levels[v-index_base]]+=1} h end |
#index_at(i) ⇒ Object
54 55 56 |
# File 'lib/rserve/rfactor.rb', line 54 def index_at(i) @ids[i] end |
#level_index(name) ⇒ Object
return the index of a given level name or -1 if it doesn’t exist
31 32 33 34 35 36 37 |
# File 'lib/rserve/rfactor.rb', line 31 def level_index(name) return nil if name.nil? levels.length.times {|i| return i+index_base if !levels[i].nil? and levels[i]==name } return nil end |
#size ⇒ Object
57 58 59 |
# File 'lib/rserve/rfactor.rb', line 57 def size @ids.length end |