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

早过忘川 提交于 2019-12-14 03:00:55

问题


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


来源:https://stackoverflow.com/questions/28531488/kdb-q-how-to-reshape-a-list-into-nrows-where-nrows-is-a-variable

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