Here's my crude solution... Happy to see any feedback on cleanup
data=: cutopen;._2 ] 0 : 0
0 p1 4
2 p1 6
3 p1 10
13 p1 15
0 p2 3
1 p2 5
7 p2 10
10 p2 11
14 p2 12
1 p3 9
5 p3 10
9 p3 20
7 p4 10
)
groupBy=: 4 : '(((~.y) i. y)) ,/. x'
d=:data groupBy (0{"1 data)
days=:(> ". each 0{"1 d)
alldays=: i. >: >./ days
missing=:(-. alldays e. days) # alldays
d=: d,(> ((3 : '6 $ <(": y)') each missing))
d=:d /: (". > 0{"1 d)
p=:(3 : '< _3[\ y')"1 d
f=: 3 : 0
t=.<y
cols=.t (4 : '> {. |. |: ((1{"1 x = <y) # x)') each ('p1';'p2';'p3';'p4')
(0{"1 {. y),cols
)
> f each p
┌──┬──┬──┬──┬──┐
│0 │4 │3 │ │ │
├──┼──┼──┼──┼──┤
│1 │ │5 │9 │ │
├──┼──┼──┼──┼──┤
│2 │6 │ │ │ │
├──┼──┼──┼──┼──┤
│3 │10│ │ │ │
├──┼──┼──┼──┼──┤
│4 │ │ │ │ │
├──┼──┼──┼──┼──┤
│5 │ │ │10│ │
├──┼──┼──┼──┼──┤
│6 │ │ │ │ │
├──┼──┼──┼──┼──┤
│7 │ │10│ │10│
├──┼──┼──┼──┼──┤
│8 │ │ │ │ │
├──┼──┼──┼──┼──┤
│9 │ │ │20│ │
├──┼──┼──┼──┼──┤
│10│ │11│ │ │
├──┼──┼──┼──┼──┤
│11│ │ │ │ │
├──┼──┼──┼──┼──┤
│12│ │ │ │ │
├──┼──┼──┼──┼──┤
│13│15│ │ │ │
├──┼──┼──┼──┼──┤
│14│ │12│ │ │
└──┴──┴──┴──┴──┘
On Fri, Jun 27, 2014 at 11:23 PM, June Kim (김창준) <[email protected]> wrote:
> The reason I use _ as a placeholder for missing data is there could be 0 as
> an authentic value.
>
> sent from an android phone
> 2014. 6. 28. 오전 2:27에 "'Pascal Jasmin' via Programming" <
> [email protected]>님이 작성:
>
> > an approach I would take: (untested)
> >
> > 'p1 p2 p3 p4' =: i.4
> >
> > (make numeric 'row column value' data) 4 : 'for_i x do. y=. v (<r;c) } y
> > [''r c v''=. i end. y' 15 4 $ 0
> >
> >
> > ----- Original Message -----
> > From: June Kim (김창준) <[email protected]>
> > To: Programming forum <[email protected]>
> > Cc:
> > Sent: Friday, June 27, 2014 1:16:03 PM
> > Subject: Re: [Jprogramming] Filling in Tables
> >
> > Hello Dan,
> >
> > On Sat, Jun 28, 2014 at 1:35 AM, Dan Bron <[email protected]> wrote:
> >
> > > June Kim asked:
> > > > Is there an easier way to do the following? My first attempt was:
> > > > 'day id val'=:|:(<a:;0 2)(".&.>@{`[`])} data
> > > > coord=:(<"1 day ,. (i.~ ~.) id)
> > > > load 'strings'
> > > > ysize=:>:>./day
> > > > (a:, <"1 ~.id), (<"0 i.ysize),.((<_);<a:) rplc~"1
> > > > <"0 val coord} _$~ysize,#~.id
> > >
> > > Part of the complexity you're wrestling with arises from your
> > construction
> > > of a boxed table with column headers and row labels. Do you really
> need
> > >
> >
> > I added the headers and labels at the end of the computation.
> >
> > that? Typically, that's only useful when you've done all your analysis
> and
> > > computation and want to present a formatted result for human
> consumption.
> > >
> >
> > I had to move the result to a spreadsheet program(in my case, Excel and
> > google spreadsheet) to create a chart. They had to be in a boxed matrix,
> > and then it could be easily transformed into a formatted string.
> >
> >
> > > If "data" represents your input, to which you want to apply some
> > analysis,
> > >
> >
> > Yes, they are.
> >
> >
> > > it would be simpler, easier, and faster to work with a pure (unboxed)
> > > numeric matrix of values, then slap the headers on when you're done.
> > >
> > >
> > I wonder how your approach would be different. Could you please show me
> how
> > you'd like to go about it with a pure matrix of values?
> >
> >
> > > If you really need the formatted boxed table, then using amend, as you
> > do,
> > > is the most common and straightforward approach. I can't really
> recommend
> > > many improvements, except perhaps to construct the body of the table
> > > directly, and skip the "rplc" part. For example:
> > >
> > > coord2tab =: verb define
> > > 'x id d'=.<"1 |: y
> > >
> > > my =. >./ y =. id i.~ uid =. ~.id
> > > mx =. >./ x =. 0 ". ;:^:_1 x
> > >
> > > t =. d (x ;&.> y)} (1 + mx,my) $ a:
> > > (a: , <"0 i. 1+mx) ,. uid , t
> > > )
> > >
> > >
> > Thank you for your idea. Now I see why rplc was redundant.
> >
> >
> > >
> > > data=: TAB cut&> LF cut noun define-.' '
> > > 0 p1 4
> > > 2 p1 6
> > > 3 p1 10
> > > 13 p1 15
> > > 0 p2 3
> > > 1 p2 5
> > > 7 p2 10
> > > 10 p2 11
> > > 14 p2 12
> > > 1 p3 9
> > > 5 p3 10
> > > 9 p3 20
> > > 7 p4 10
> > > )
> > >
> > >
> > > coord2tab data
> > > +--+--+--+--+--+
> > > | |p1|p2|p3|p4|
> > > +--+--+--+--+--+
> > > |0 |4 |3 | | |
> > > +--+--+--+--+--+
> > > |1 | |5 |9 | |
> > > +--+--+--+--+--+
> > > |2 |6 | | | |
> > > +--+--+--+--+--+
> > > |3 |10| | | |
> > > +--+--+--+--+--+
> > > |4 | | | | |
> > > +--+--+--+--+--+
> > > |5 | | |10| |
> > > +--+--+--+--+--+
> > > |6 | | | | |
> > > +--+--+--+--+--+
> > > |7 | |10| |10|
> > > +--+--+--+--+--+
> > > |8 | | | | |
> > > +--+--+--+--+--+
> > > |9 | | |20| |
> > > +--+--+--+--+--+
> > > |10| |11| | |
> > > +--+--+--+--+--+
> > > |11| | | | |
> > > +--+--+--+--+--+
> > > |12| | | | |
> > > +--+--+--+--+--+
> > > |13|15| | | |
> > > +--+--+--+--+--+
> > > |14| |12| | |
> > > +--+--+--+--+--+
> > >
> > > But as I said, this is fundamentally the same approach, with minor
> > > simplification and some reformatting of the code.
> > >
> > > -Dan
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> >
> >
> > >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm