kdb/q: how to reshape a list into nRows, where nRo

2019-08-16 04:01发布

问题:

If I am to split a list into 2 rows, I can use:

q)2 0N#til 10

However, the following syntax does not work:

q)n:2
q)n 0N#til 10

how I can achieve such reshaping?

回答1:

Need brackets and semi colon

q)2 0N#til 10
0 1 2 3 4
5 6 7 8 9
q)n:2
q)(n;0N)#til 10
0 1 2 3 4
5 6 7 8 9


回答2:

Here is the general syntax to split a list in matrix form:

               (list1)#(list2)

As you can see, left part and right part of '#' is list. So here is one example:

       q)list1: (4;3)    / or simply (4 3)
       q)list2: til 12
       q)list1#list2

We can make an integer list in 2 way:

  1. Using semicolon as list1:(2;3;4)
  2. Using spaces as list1:(2 3 4)

But when you have variable, option 2 doesn't work;

       q)list1: (n 3)    / where n:2
       q) `type error

So for your question, solution is to use semicolon to create list:

       q) list1:(n;0N)
       q) list1#til 10


标签: kdb