سؤال

I'd like to calculate a new column which is a function of several columns using select.

My actual application will involve a grouping in the select so the columns entries which I will pass to the function will contain lists. But this simple example illustrates my question

t:([] a:1 2 3; b:10 20 30; c:5 6 7)

/ Pass one argument, using projection (set first two arguments to 1)
select s:{[x;y;z] x+y+z}[1;1;] each a from t 

/ Pass two arguments using each-both (set first arg to 1)
select s:a {[x;y;z] x+y+z}[1;;]'b from t 

Now, how can I pass three or more arguments?

هل كانت مفيدة؟

المحلول

Each' will work in general but it's best to use vector operations where possible. Here I use the . operator to apply our function, \t to time both methods. I store their results to r1/r2 to show they are the same:

q)t:([]a:til n;b:til n;c:til n:1200300)
q)\t r1:update d:{x+y+z}'[a;b;c] from t
289
q)\t r2:update d:{x+y+z} . (a;b;c) from t
20
q)r1~r2
1b
q)r2
a  b  c  d
-----------
0  0  0  0
1  1  1  3
2  2  2  6
3  3  3  9
4  4  4  12
5  5  5  15
..

Cheers, Ryan

نصائح أخرى

The following form works in general

q)t:([]a:til 10;b:til 10;c:til 10)
q)select d:{x+y+z}'[a;b;c] from t
d
--
0
3
6
9
..
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top