我有一组时间段和值:
2013-03-04 02:00:00 UTC
2013-03-04 02:01:00 UTC
2013-03-04 02:02:00 UTC
2013-03-04 02:03:00 UTC
2013-03-04 02:04:00 UTC
2013-03-04 02:05:00 UTC
2013-03-04 02:06:00 UTC
2013-03-04 02:07:00 UTC
2013-03-04 02:08:00 UTC
2013-03-04 02:09:00 UTC
2013-03-04 02:10:00 UTC
2013-03-04 02:11:00 UTC
2013-03-04 02:12:00 UTC
2013-03-04 02:13:00 UTC
2013-03-04 02:14:00 UTC
2013-03-04 02:15:00 UTC
2013-03-04 02:16:00 UTC
2013-03-04 02:17:00 UTC
2013-03-04 02:18:00 UTC
2013-03-04 02:19:00 UTC
2013-03-04 02:20:00 UTC
2013-03-04 02:21:00 UTC
2013-03-04 02:22:00 UTC
2013-03-04 02:23:00 UTC
2013-03-04 02:24:00 UTC
2013-03-04 02:25:00 UTC
2013-03-04 02:26:00 UTC
2013-03-04 02:27:00 UTC
2013-03-04 02:28:00 UTC
2013-03-04 02:29:00 UTC
2013-03-04 02:30:00 UTC
每个时间段都有自己的浮点数值。就像:
1.192069669
1.398097386
1.51857279
1.667887277
1.919998368
2.007405113
2.079283295
2.160574819
2.184922836
2.194639015
2.234917032
2.278915283
2.307196654
2.265553703
2.209039541
2.204034995
2.271997186
2.192411672
2.268617205
2.136996194
2.20568788
2.202653527
2.186529626
2.206325387
2.182503664
1.196174184
1.206075579
1.226468123
1.177213548
1.060684314
1.215226185
1.268223732
1.334227719
1.503234406
1.699964124
1.680645046
1.767723116
1.733957416
1.79358097
1.847852493
1.918378765
1.95619668
1.900485186
1.908825891
2.138246816
2.170221961
2.131884782
2.140921904
2.171028766
2.392254099
2.497088696
问题是:
我需要总结每 15 分钟的值并取它们的算术平均值。
例如 02:00 和 02:14 之间的数据是第一组 02:15 和 02:29 是第二组。02:30 和 02:44 .....
我需要每组的平均值。
必须使用for
或while
使用 15.minutes 时间助手来完成,但我无法使其工作。
如何处理这些?
谢谢你。
编辑:
数组结构是这样的:
ras_date tv1_rating tv2_rating tv3_rating
------------------------------------------------------------------------------------
2013-03-04 02:00:00 UTC | 2.184922836 | 2.278915283 | 2.278915283
2013-03-04 02:01:00 UTC | 2.194639015 | 3.278915283 | 1.278915283
2013-03-04 02:02:00 UTC | 0.234917032 | 4.278915283 | 0.278915283
2013-03-04 02:03:00 UTC | 2.278915283 | 1.278915283 | 2.278915283
..
.
我想根据 ras_date 计算每个电视台 15 分钟的平均收视率。
好吧,使用 for 和 while 只是我的一个猜测。欢迎任何类型的解决方案。谢谢你。
编辑2:
[#<DataFile id: 48009, timeband: \"02:00\", ras_date: \"2013-03-04 02:00:00\", created_at: \"2013-03-05 18:45:16\", updated_at: \"2013-03-05 18:45:16\", tv1_rtg: 0.231275974, tv1_shr: 3.459938790606675, tv2_rtg: 0.465407082, tv2_shr: 6.9625910058208715, tv4_rtg: 0.024855499, tv4_shr: 0.3718436621954748, tv5_rtg: 0.355105523, tv5_shr: 5.3124557321564705, tv3_rtg: 0.306065103, tv3_shr: 4.578800400255703, tv6_rtg: 0.425388078, tv6_shr: 6.363898016202141, tv8_rtg: 0.184050299, tv8_shr: 2.7534324379620037, tv7_rtg: 0.13351472, tv7_shr: 1.9974092027604597, tv8_rtg: 0.104982397, tv8_shr: 1.5705594551346256, ttv_rtg: 6.684394956, ttv_shr: 100.0, tv9_shr: 3.8946589139877266, tv9_rtg: 0.260334384>, #<DataFile id: 48010, timeband: \"02:01\", ras_date: \"2013-03-04 02:01:00\", created_at: \"2013-03-05 18:45:16\", updated_at: \"2013-03-05 18:45:16\", tv1_rtg: 0.111862045, tv1_shr: 1.6818552137556582, tv2_rtg: 0.464504362, tv2_shr: 6.983861979654901, tv4_rtg: 0.057792275, tv4_shr: 0.8689116940741676, tv5_rtg: 0.353541524, tv5_shr: 5.315526418442655, tv3_rtg: 0.315364133, tv3_shr: 4.741526147833099, tv6_rtg: 0.463173951, tv6_shr: 6.963859138863031, tv8_rtg: 0.188181065, tv8_shr: 2.8293180703101073, tv7_rtg: 0.148338388, tv7_shr: 2.2302800852416897, tv8_rtg: 0.112267205, tv8_shr: 1.6879468282832242, ttv_rtg: 6.651110279, ttv_shr: 100.0, tv9_shr: 3.673313247735431, tv9_rtg: 0.244316115>, #<DataFile id: 48011, timeband: \"02:02\", ras_date: \"2013-03-04 02:02:00\", created_at: \"2013-03-05 18:45:16\", updated_at: \"2013-03-05 18:45:16\", tv1_rtg: 0.096118706, tv1_shr: 1.4904838004557073, tv2_rtg: 0.482073193, tv2_shr: 7.475363690398179, tv4_rtg: 0.045212074, tv4_shr: 0.7010900030427445, tv5_rtg: 0.367055659, tv5_shr: 5.691821460903709, tv3_rtg: 0.336406586, tv3_shr: 5.2165555245782205, tv6_rtg: 0.463173951, tv6_shr: 7.18229884158621, tv8_rtg: 0.175622569, tv8_shr: 2.72332623879596, tv7_rtg: 0.13351472, tv7_shr: 2.0703725171079563, tv8_rtg: 0.109862708, tv8_shr: 1.7036078965544506, ttv_rtg: 6.448825943, ttv_shr: 100.0, tv9_shr: 4.214133803611018, tv9_rtg: 0.271762154>, #<DataFile id: 48012, timeband: \"02:03\", ras_date: \"2013-03-04 02:03:00\", created_at: \"2013-03-05 18:45:16\", updated_at: \"2013-03-05 18:45:16\", tv1_rtg: 0.111568925, tv1_shr: 1.7336392363465083, tv2_rtg: 0.589141334, tv2_shr: 9.154507246313642, tv4_rtg: 0.100518614, tv4_shr: 1.5619314537051374, tv5_rtg: 0.402849844, tv5_shr: 6.259774358446762, tv3_rtg: 0.328333577, tv3_shr: 5.101886315541693, tv6_rtg: 0.43495935, tv6_shr: 6.758715254949115, tv8_rtg: 0.175622569, tv8_shr: 2.7289514208940524, tv7_rtg: 0.13351472, tv7_shr: 2.0746489869093736, tv8_rtg: 0.102741203, tv8_shr: 1.5964676607777801, ttv_rtg: 6.435532991, ttv_shr: 100.0, tv9_shr: 1.7713521655381412, tv9_rtg: 0.113995953>]
编辑3:
我的数组文件如上所示(见edit-2)。
当我尝试时:
d = DataFile.all
d.each_slice(15) { |v| puts v.inject(0.0) { |sum, el| sum + el } / v.size }
我得到这个错误:
TypeError: DataFile can't be coerced into Float
from (irb):31:in `+'
from (irb):31:in `block (2 levels) in irb_binding'
from (irb):31:in `each'
from (irb):31:in `inject'
from (irb):31:in `block in irb_binding'
from (irb):31:in `each'
from (irb):31:in `each_slice'
from (irb):31