I've tried to define a verb melt that generalises the technique for an
arbitrary number of ID columns. It seems to work except that I have an
extra level of boxing around the ID columns. How best to remove them? (I
figure once that's solved then the labels will match at the top OK too!)

   tst=.a: (<0;0)}(;/20+i.6),((;/100+i.4),.<"0 i.4 5)

NB. =========================================================
NB.*melt v reshape table to database style
NB. optional left arg for number of "label" columns
melt=: 3 : 0
        x=.1
        x melt y
:
        lbls=. (x{.{.y),;:'variable value'
        a=. }.(i.x){"1 y
        b=. x}.0{ y
        c=. x}."1 }. y
        lbls,(>,{(<"1 a);<b),.,c
)

   2 melt tst
+--------+--+--------+-----+
|id      |21|variable|value|
+--------+--+--------+-----+
|+---+-+ |22|1       |     |
||100|0| |  |        |     |
|+---+-+ |  |        |     |
+--------+--+--------+-----+
|+---+-+ |23|2       |     |
||100|0| |  |        |     |
|+---+-+ |  |        |     |
+--------+--+--------+-----+
...
+--------+--+--------+-----+
|+---+--+|25|19      |     |
||103|15||  |        |     |
|+---+--+|  |        |     |
+--------+--+--------+-----+

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Chris Burke
Sent: Thursday, 8 February 2007 16:14
To: Programming forum
Subject: Re: [Jprogramming] reshaping array

Sherlock, Ric wrote:
> Thanks for the answers, I'll add this to my study material By the way 
> Chris' answer doesn't work in its current form when the array is 
> boxed. Good exercise for me to see if I can work out why!

   a=: <@>100+i.4
   b=: <@>21+i.5
   c=: <@>i.4 5
   (>,{a;<b),.,c
+---+--+--+
|100|21|0 |
+---+--+--+
|100|22|1 |
+---+--+--+
|100|23|2 |
+---+--+--+
|100|24|3 |
+---+--+--+
...
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to