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

2019-08-16 03:33发布

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?

标签: kdb
2条回答
一夜七次
2楼-- · 2019-08-16 03:55

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
查看更多
戒情不戒烟
3楼-- · 2019-08-16 03:56

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
查看更多
登录 后发表回答