使用值列表将列动态合并为一列

Dynamically consolidate columns into one column with a list of values

我有一个 table idx,每次我构建它时,它都可以由许多标记为 `A `B `C 的分组列组成。通常有大约 2 或 3 个这样的列(所以 `A `B 或 `A `B `C)但理论上可能更多。还有一个时间戳列`t 和一个浮点列`rslt.

我希望重铸分组列,以便我有 1 列包含以前分组列中的值列表。对于固定数量的列(比如 2 - `A `B),我会使用:

?[`idx;();0b;`c`t`rslt!((+:;(enlist;(`idx;enlist`A);(`idx;enlist`B)));`t;`rslt)]

但是,由于分组列的数量可能会有所不同,我曾希望替换

(`idx;enlist`A);(`idx;enlist`B)

{enlist (`idx;enlist x)} each aCols

其中 aCols 是列的列表。 (所以 aCols:`A`B

?[`idx;();0b;`c`rslt!((+:;(enlist;{(`idx;enlist x),} each aCols));`rslt)]

这没有用。

我注意到解析函数显示开发人员 GUI 中未呈现括号,因此我的代码给出:

`idx ,`A
`idx ,`B

而不是 'static' 函数给出:

(`idx ,`A)
(`idx ,`B)

这是问题的根源吗?为什么我构建的列表在控制台打印输出中没有用左括号和右括号划界?

你能告诉我哪里可能出错了吗?

感谢和问候,

西蒙

这不是您问题的答案,但您无需在此处进行功能更新即可获得您想要的结果。请注意,我已经猜测了以下 table.

的数据
q)show idx:([]t:5?.z.p;rslt:5?10f;A:5?10;B:5?10;C:5?10)
t                             rslt     A B C
--------------------------------------------
2010.12.31D03:08:15.054082816 2.124007 9 0 8
2009.05.26D07:50:10.992718592 7.77882  5 9 6
2000.03.01D12:40:41.350664308 4.844727 9 5 9
2002.02.19D06:32:27.078053856 6.827999 7 2 0
2002.02.23D03:39:47.650230800 1.53227  6 3 0
q)f:{(y _x),'flip enlist[`c]!enlist flip x y:(),y}
q)f[idx;`A`B`C]
t                             rslt     c
--------------------------------------------
2010.12.31D03:08:15.054082816 2.124007 9 0 8
2009.05.26D07:50:10.992718592 7.77882  5 9 6
2000.03.01D12:40:41.350664308 4.844727 9 5 9
2002.02.19D06:32:27.078053856 6.827999 7 2 0
2002.02.23D03:39:47.650230800 1.53227  6 3 0
q)f[delete A,B from idx;`C]
t                             rslt     c
----------------------------------------
2010.12.31D03:08:15.054082816 2.124007 8
2009.05.26D07:50:10.992718592 7.77882  6
2000.03.01D12:40:41.350664308 4.844727 9
2002.02.19D06:32:27.078053856 6.827999 0
2002.02.23D03:39:47.650230800 1.53227  0

如果数据看起来像:

idx:([]t:5?.z.p;A:5?.Q.A;B:5?.Q.A;C:5?.Q.A;rslt:5?10f)

那么你的泛函可以变成:

?[`idx;();0b;`c`t`rslt!((+:;enlist,{(`idx;enlist x)}each aCols);`t;`rslt)]

但更简洁的函数式方法可能是

q)?[`idx;();0b;`c`t`rslt!((,';;)/[`A`B`C];`t;`rslt)]
c     t                             rslt
--------------------------------------------
"NGJ" 2009.01.10D17:16:43.689503360 3.927524
"JBL" 2011.11.04D08:07:14.231674368 5.170911
"CYF" 2012.03.03D11:18:21.556469184 5.159796
"HFU" 2017.06.10D02:01:53.890645696 4.066642
"AEW" 2008.01.01D00:10:18.538771776 1.780839