Hi there,
I’d like to know how I could apply the same function to multiple columns on one table. Let’s say I have a function
cg:{(rand count x)?x}
And table
t:([] a:("aaa";"aab";"aac");b:("baa";"bab";"bac");c:("caa";"cab";"cac")) a b c-----------------"aaa" "baa" "caa""aab" "bab" "cab""aac" "bac" "cac"
And I would like to get the following
a b c--------------"" ,"a" "ac""" "ab" ,"b""ac" ,"c" ""
without having to use
select a:cg each a, b:cg each b, c:cg each c from t
Obviously there could be a case where I have 10 columns with which I wish to apply the same function…
Regards,
Zak
Assuming the table is in-memory, for neatness you could do
cg each’ t
If the table is long then it might be a bit better performance to do
flip cols[t]!cg each’ t cols t
(as it works column-at-a-time rather than row-at-a-time)
Thanks
Jonny
AquaQ Analytics
sohagan
3
for future help…
http://code.kx.com/wiki/JB:QforMortals2/queries\_q\_sql#Functional\_update
cg:{(rand count x)?x}’ //note the each-both
Then update
![t;();0b;{x!{(cg;x)}each x}`a`b`c]
This will be quicker but harder on memory in this case.
Thanks Jonny. It works great.
However let’s say I only want to apply the function but not to all the columns (let’s say 7 out of 10 columns), how would that work out?
Zak
Actually, that second version is a bit long winded. You can do this instead which would also cover your new case:
@[t;a
b;cg’]
(where a
b is the list of columns you want to amend)
Works like a charm, thank you!
Zak
using functional select.
?[t;();0b;cols[
t]!({cg each x},/:cols t)]