how to use ungroup with multiple composite columns in kdb

[亡魂溺海] 提交于 2021-02-08 06:34:30

问题


Consider the following table:

`Keys  `Values
`A`B   `V1`V2
`C`D`E   `V1`V2`V3`V4

I want to flatten the Keys column only such that each key is mapped to the corresponding Values. The result should be:

`Keys  `Values
`A     `V1`V2
`B     `V1`V2
`C     `V1`V2`V3`V4
`D     `V1`V2`V3`V4
`E     `V1`V2`V3`V4

ungroup function applies ungroup to all columns, in this case I want to apply to Keys column alone.


回答1:


Few different ways you can go about this, below is one way -

ungroupCol:{[tbl;col]
    @[tbl where count each tbl col;col;:;raze tbl col]
}

Then:

q)t:flip `Keys`Values!((`A`B;`C`D`E);(`V1`V2;`V1`V2`V3`V4))
q)ungroupCol[t;`Keys]
    Keys Values
    -----------------
    A    `V1`V2
    B    `V1`V2
    C    `V1`V2`V3`V4
    D    `V1`V2`V3`V4
    E    `V1`V2`V3`V4


来源:https://stackoverflow.com/questions/39314493/how-to-use-ungroup-with-multiple-composite-columns-in-kdb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!