15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'app/transforms/moving_average.rb', line 15
def result
cols = self.columns
datecol = self.date_column
daycount = self.num_days
avg_cols = cols.collect { |col| "#{col}_#{daycount}" }
out = Ruport::Data::Table.new( :column_names => [datecol] + cols + avg_cols)
data = day_data
days = data["days"]
first = data["earliest"]
latest = data["latest"]
current = first
while current <= latest do
row = {}
row[datecol] = current
cols.each do |col|
value = 0
value = days[current][col].to_f if days[current]
sum = 0
count = 0
check = current - daycount + 1
while check <= current
count += 1 unless check < first
sum += days[check][col].to_f if days[check]
check += 1
end
row["#{col}_#{daycount}"] = sum / count
row["#{col}"] = value
end
out << row
current += 1
end
out
end
|