创建一个布尔列,显示 kdb+ 中其他 2 个列之间的比较

Create a Boolean column displaying comparison between 2 other columns in kdb+

我目前正在学习 kdb+/q。 我有 table 的数据。我想获取 2 列数据(只是数字),比较它们并创建一个新的布尔列,该列将显示第 1 列中的值是否大于或等于第 2 列中的值。

我很乐意table使用更新命令创建一个新列,但我不知道如何确保它是布尔值,如何比较值以及显示 "greater-than-or-equal-to-ness" - 是否可以为此做一个简单的 Y/N 输出?

谢谢。

/ dummy data
q) show t:([] a:1 2 3; b: 0 2 4)
    a b
    ---
    1 0
    2 2
    3 4

/ add column name 'ge' with value from b>=a
q) update ge:b>=a from t
    a b ge
    ------
    1 0 0
    2 2 1
    3 4 1

使用条件向量: http://code.kx.com/q/ref/lists/#vector-conditional

    q)t:([]c1:1 10 7 5 9;c2:8 5 3 4 9)
    q)r:update goe:?[c1>=c2;1b;0b] from t
    c1 c2 goe
    -------------
    1  8  0
    10 5  1
    7  3  1
    5  4  1
    9  9  1

使用meta确认goe列是boolean类型:

  q)meta r
   c      | t f a
   -------| -----
   c1     | j
   c2     | j
   goe    | b

运算符 <= 适用于向量,但在某些情况下,当函数需要原子作为执行运算的输入时,您可能需要使用 'each-both 运算符).

例如将符号字符串的长度与另一列值进行比较

q)f:{x<=count string y}
q)f[3;`ab]
0b

q)t:([] l:1 2 3; s: `a`bc`de)
q)update r:f'[l;s] from t
l s  r
------
1 a  1
2 bc 1
3 de 0