On Mon, Jun 30, 2014 at 10:54 PM, Dan Bron <[email protected]> wrote:
> My wording was ambiguous - sorry about that. The fundamental approach
would
> be the same whether the data are numeric or boxed or anything else. That
> is, the natural and straightforward way to create a value table from a
> list of index-value pairs is to first construct a table of placeholders,
> and then populate it with valid values using } .
Another natural way of generating a value table uses key.
You wind up having to use key, also, if you somehow have multiple relevant
values for the same index.
What you do is prefix your index-value pairs with the full set of indices
in canonical order each paired with the default. And your key function
deals with each instance of the resulting sequences.
For this problem, you would wind up doing that twice - first to form what
will be the rows, and then again to form the columns.
Then again, for this example, we don't actually have any actual cases where
we have multiple values that go in the same row, nor in the same column, so
this approach might be overkill. Nevertheless, here's an example of what
this kind of implementation might look like:
ids=: /:~ ~.1 {"1 data
dts=: ":&.> i.1+>./_ ".&>{."1 data
gather=:2 :0
:
({."1 u@}./. v"1)(x,.a:),y
)
dts (ids {. gather {: ])gather}. data
┌──┬──┬──┬──┐
│4 │3 │ │ │
├──┼──┼──┼──┤
│ │5 │9 │ │
├──┼──┼──┼──┤
│6 │ │ │ │
├──┼──┼──┼──┤
│10│ │ │ │
├──┼──┼──┼──┤
│ │ │ │ │
├──┼──┼──┼──┤
│ │ │10│ │
├──┼──┼──┼──┤
│ │ │ │ │
├──┼──┼──┼──┤
│ │10│ │10│
├──┼──┼──┼──┤
│ │ │ │ │
├──┼──┼──┼──┤
│ │ │20│ │
├──┼──┼──┼──┤
│ │11│ │ │
├──┼──┼──┼──┤
│ │ │ │ │
├──┼──┼──┼──┤
│ │ │ │ │
├──┼──┼──┼──┤
│15│ │ │ │
├──┼──┼──┼──┤
│ │12│ │ │
└──┴──┴──┴──┘
Breaking that down:
dts (ids {. gather {: ])gather}. data
I'm using }. when gathering the rows because the "dates" are the first
column of 'data'. So here's a perspective on what the inner gather deals
with.
dts <gather}. data
(Run this yourself - it would line wrap in email, rendering it illegible.)
The inner gather then uses {: to grab the final element from each row of
data, this becomes the data in the result.
Also, I'm using {. for the inner gather to discard any extra values. If I
had multiple values to combine, this would be the combining function. If I
wanted to be robust I'd probably instead use something like <@(;:inv) for
this kind of data.
FYI,
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm