问题
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