Emily Wilson
Emily Wilson

Reputation: 3

Replace first n entries in a column in kdb

How can I replace the values in the first n columns of my table?

i.e. mycol:(1 2 3 4) to mycol:(a a 3 4)

Thank you in advance!

Upvotes: 0

Views: 286

Answers (1)

Cathal O'Neill
Cathal O'Neill

Reputation: 3179

If it's the values within mycol that you want updated then they will need to be of the same type as the existing values. See below.

q)t:([]mycol:`$string 1+til 4;mycol2:til 4)
q)update mycol:`a from t where i<2
mycol mycol2
------------
a     0
a     1
3     2
4     3

One way around this though is to enlist mycol, that way updates of any type can be made.

q)t:([]mycol:1+til 4;mycol2:til 4)
q)update mycol:`a from(update enlist each mycol from t)where i<2
mycol mycol2
------------
`a    0
`a    1
,3    2
,4    3
q)meta update mycol:`a from(update enlist each mycol from t)where i<2
c     | t f a
------| -----
mycol |
mycol2| j

It's unclear from your question whether you want the column names or the column values changed. If it's the column names, you can use xcol.

q)(2#`a)xcol([]w:3#til 3;x:3#.Q.a;y:`;z:0N)
a a y z
-------
0 a
1 b
2 c

Upvotes: 1

Related Questions