Class: Daru::DataFrame

Inherits:
Object show all
Defined in:
lib/statsample/daru.rb

Instance Method Summary collapse

Instance Method Details

#crosstab(v1, v2, opts = {}) ⇒ Object



48
49
50
# File 'lib/statsample/daru.rb', line 48

def crosstab(v1,v2,opts={})
  Statsample::Crosstab.new(self[v1], self[v2],opts)
end

#to_multiset_by_split(*vecs) ⇒ Object

Functions for converting to Statsample::Multiset



53
54
55
56
57
58
59
60
61
# File 'lib/statsample/daru.rb', line 53

def to_multiset_by_split(*vecs)
  require 'statsample/multiset'

  if vecs.size == 1
    to_multiset_by_split_one_field(vecs[0])
  else
    to_multiset_by_split_multiple_fields(*vecs)
  end
end

#to_multiset_by_split_multiple_fields(*fields) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/statsample/daru.rb', line 82

def to_multiset_by_split_multiple_fields(*fields)
  fields.map!(&:to_sym)
  factors_total=nil
  fields.each do |f|
    if factors_total.nil?
      factors_total = self[f].factors.collect { |c| [c] }
    else
      suma = []
      factors = self[f].factors
      factors_total.each do |f1| 
        factors.each do |f2| 
          suma.push(f1+[f2])
        end
      end
      factors_total = suma
    end
  end
  ms = Statsample::Multiset.new_empty_vectors(vectors.to_a, factors_total)

  p1 = eval "Proc.new {|c| ms[["+fields.collect{|f| "c['#{f}'.to_sym]"}.join(",")+"]].add_row(c) }"
  each_row { |r| p1.call(r) }

  ms.datasets.each do |k,ds|
    ds.update 
    ds.rename(
      fields.size.times.map do |i|
        f  = fields[i]
        sk = k[i]
        self[f].index_of(sk)
      end.join("-")
    )
  end
  ms
end

#to_multiset_by_split_one_field(field) ⇒ Object

Creates a Statsample::Multiset, using one field

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/statsample/daru.rb', line 64

def to_multiset_by_split_one_field(field)
  raise ArgumentError,"Should use a correct field name" if 
    !@vectors.include? field

  factors = self[field].factors
  ms      = Statsample::Multiset.new_empty_vectors(@vectors.to_a, factors)
  each_row do |row|
    ms[row[field]].add_row(row)
  end
  #puts "Ingreso a los dataset"
  ms.datasets.each do |k,ds|
    ds.update
    ds.rename self[field].index_of(k)
  end

  ms
end