I have a multidimensional array similar to the example below, which I want to group using the Ruby zip method. I work fine when each inner array has the same number of elements, but I run into problems when they are different.
In the example below, in the second set there is no entry at 00:15. How to fill in this missing entry?
What do I consider as a space?
This is a timestamp that represents a gap. Take a look at my first sample code, where I have a comment about the gap is 00:15. All other arrays have a hash with this timestamp, so I consider this to be a “missing entry” or a “space”. timestamp can really be some other unique string, so the fact that they are 15 minutes apart doesn't matter. Values also do not matter.
The only approach that comes to mind is to iterate over arrays twice. The first time it would be necessary to build an array of uniq timestamps, and the second time - to fill in the missing record (s), where there is no timestamp. I find it more convenient to code this approach, but it seems a bit hacky, and Ruby always surprises me with an elegant and concise solution.
I'll start with this:
values = [
[
{:timestamp => "2011-01-01 00:00", :value => 1},
{:timestamp => "2011-01-01 00:15", :value => 2},
{:timestamp => "2011-01-01 00:30", :value => 3}
],
[
{:timestamp => "2011-01-01 00:00", :value => 1},
{:timestamp => "2011-01-01 00:30", :value => 3}
],
[
{:timestamp => "2011-01-01 00:00", :value => 1},
{:timestamp => "2011-01-01 00:15", :value => 2},
{:timestamp => "2011-01-01 00:30", :value => 3}
]
]
I want to end with this:
values = [
[
{:timestamp => "2011-01-01 00:00", :value => 1},
{:timestamp => "2011-01-01 00:15", :value => 2},
{:timestamp => "2011-01-01 00:30", :value => 3}
],
[
{:timestamp => "2011-01-01 00:00", :value => 1},
{:timestamp => "2011-01-01 00:15", :value => nil},
{:timestamp => "2011-01-01 00:30", :value => 3}
],
[
{:timestamp => "2011-01-01 00:00", :value => 1},
{:timestamp => "2011-01-01 00:15", :value => 2},
{:timestamp => "2011-01-01 00:30", :value => 3}
]
]
, values.transpose :
[
[
{:value=>1, :timestamp=>"2011-01-01 00:00"},
{:value=>1, :timestamp=>"2011-01-01 00:00"},
{:value=>1, :timestamp=>"2011-01-01 00:00"}
],
[
{:value=>2, :timestamp=>"2011-01-01 00:15"},
{:value=>nil, :timestamp=>"2011-01-01 00:15"},
{:value=>2, :timestamp=>"2011-01-01 00:15"}
],
[
{:value=>3, :timestamp=>"2011-01-01 00:30"},
{:value=>3, :timestamp=>"2011-01-01 00:30"},
{:value=>3, :timestamp=>"2011-01-01 00:30"}
]
]