q-在表格上逐行应用功能 [英] q - apply function on table rowwise
问题描述
给出一个表和一个函数
t:([] c1:1 2 3; c2:`a`b`c; c3:13:00 13:01 13:02)
f:{[int;sym;date]
symf:{$[x=`a;1;x=`b;2;3]};
datef:{$[x=13:00;1;x=13:01;2;3]};
r:int + symf[sym] + datef[date];
r
};
我注意到,当将函数f
应用到t
的列时,则整个列都将传递到f
中,如果可以原子操作它们,则输出的长度将与输入的长度相同并生成一个新的列.但是,在我们的示例中,此操作无效:
I noticed that when applying the function f
onto columns of t
, then the entire columns are passed into f
and if they can be operated on atomically then the output will be of the same length as the inputs and a new column is produced. However in our example this wont work:
update newcol:f[c1;c2;c3] from t / 'type error
因为内部函数symf
和datef
不能分别应用于整个列c2
,c3
.
because the inner functions symf
and datef
cannot be applied to the entire column c2
, c3
, respectively.
如果我完全不要更改函数f,那么如何逐行应用它并将值收集到t
中的新列中.
If I dont want to change the function f at all, how can I apply it row by row and collect the values into a new column in t
.
最q
样式的方法是什么?
编辑
如果不更改f
确实很不方便,则可以这样解决
If not changing f
is really inconvenient one could workaround like so
f:{[arglist]
int:arglist 0;
sym:arglist 1;
date:arglist 2;
symf:{$[x=`a;1;x=`b;2;3]};
datef:{$[x=13:00;1;x=13:01;2;3]};
r:int + symf[sym] + datef[date];
r
};
f each (t`c1),'(t`c2),'(t`c3)
还是,我会很感兴趣在使用f
的原始版本时如何获得相同的结果
Still I would be interested how to get the same result when working with the original version of f
谢谢!
推荐答案
您可以使用但是,通过将f修改为矢量化",您可能会获得更好的性能.
However you will likely get better performance by modifying f to be "vectorised" e.g.
q)f2
{[int;sym;date]
symf:3^(`a`b!1 2)sym;
datef:3^(13:00 13:01!1 2)date;
r:int + symf + datef;
r
}
q)update newcol:f2[c1;c2;c3] from t
c1 c2 c3 newcol
------------------
1 a 13:00 3
2 b 13:01 6
3 c 13:02 9
q)\ts:1000 update newcol:f2[c1;c2;c3] from t
4 1664
q)\ts:1000 update newcol:f'[c1;c2;c3] from t
8 1680
通常在KDB中,如果您可以避免使用每种形式的任何形式并坚持使用向量运算,那么您将获得更高的效率
In general in KDB, if you can avoid using any form of each and stick to vector operations, you'll get much more efficiency
这篇关于q-在表格上逐行应用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!