# How can I apply a function with 'if' to a table?

**URL:** https://forum.kx.com/t/how-can-i-apply-a-function-with-if-to-a-table/13305
**Category:** Community Support
**Tags:** kdb-and-q, imported
**Created:** [February 1, 2024, 12:00am UTC](https://forum.kx.com/t/how-can-i-apply-a-function-with-if-to-a-table/13305 "2024-02-01T00:00:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![voiddump](https://avatars.discourse-cdn.com/v4/letter/v/d2c977/32.png) [@voiddump](https://forum.kx.com/u/voiddump)
#### Post date: [February 1, 2024, 12:00am UTC](https://forum.kx.com/t/how-can-i-apply-a-function-with-if-to-a-table/13305/1 "2024-02-01T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/how-can-i-apply-a-function-with-if-to-a-table](https://learninghub.kx.com/forums/topic/how-can-i-apply-a-function-with-if-to-a-table)

Hi! I’m trying to apply a function to a table. The basic idea is that if the 2 prices adds up to 0, then the new column gets 0, otherwise it gets the product of the 2 prices. However I encountered _' **type** _ error.

&nbsp;

The codes I wrote were?

```
//This is for creating t1

t1:([] TimeStamp:2018.01.01 2018.01.01 2018.01.02 2018.01.02;stock:`AAPL`AAPL`GOOGL`GOOGL;p1: 1 2 -2 3;p2: -1 1 2 0)

//Then I define a function

f:{[a;b] if[(a+b)=0;0;a*b]}

//Then I apply

t1:update r:f[p1;p2] from t1
```

&nbsp;

However, it gave error message:

```
'type[4]f:{[a;b] if[(a+b)=0;0;a*b]}
```

_^_

&nbsp;

How can I solve this problem?

&nbsp;

---

<div class="post-metadata">

### Author: ![amcnaught921](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@amcnaught921](https://forum.kx.com/u/amcnaught921)
#### Post date: [February 1, 2024, 12:00am UTC](https://forum.kx.com/t/how-can-i-apply-a-function-with-if-to-a-table/13305/2 "2024-02-01T00:00:00Z")

</div>

Hey,

&nbsp;

The following will give the solution you require

```
q)t1
TimeStamp stock p1 p2
----------------------
2018.01.01 AAPL 1 -1
2018.01.01 AAPL 2 1
2018.01.02 GOOGL -2 2
2018.01.02 GOOGL 3 0

q)f:{$[0=x+y;0;x\*y]}q)t1:update r:f'[p1;p2] from t1

TimeStamp stock p1 p2 r
------------------------
2018.01.01 AAPL 1 -1 0
2018.01.01 AAPL 2 1 2
2018.01.02 GOOGL -2 2 0
2018.01.02 GOOGL 3 0 0
```

Some documentation on $ also here: [https://code.kx.com/q/ref/cond/](https://code.kx.com/q/ref/cond/")

---

<div class="post-metadata">

### Author: ![acapper](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@acapper](https://forum.kx.com/u/acapper)
#### Post date: [February 1, 2024, 12:00am UTC](https://forum.kx.com/t/how-can-i-apply-a-function-with-if-to-a-table/13305/3 "2024-02-01T00:00:00Z")

</div>

This can be done entirely within the update statement, using a vector conditional.

```
q)t1:([] TimeStamp:2018.01.01 2018.01.01 2018.01.02 2018.01.02;stock:`AAPL`AAPL`GOOGL`GOOGL;p1: 1 2 -2 3;p2: -1 1 2 0)
q)update r:?[(p1+p2)=0;0;p1*p2] from t1
TimeStamp stock p1 p2 r
------------------------
2018.01.01 AAPL 1 -1 0
2018.01.01 AAPL 2 1 2
2018.01.02 GOOGL -2 2 0
2018.01.02 GOOGL 3 0 0
```

&nbsp;

With further reading found [here](https://code.kx.com/q/ref/vector-conditional/") on vector conditionals.

&nbsp;

---

<div class="post-metadata">

### Author: ![cillianreilly](https://avatars.discourse-cdn.com/v4/letter/c/7bcc69/32.png) [@cillianreilly](https://forum.kx.com/u/cillianreilly)
#### Post date: [February 1, 2024, 12:00am UTC](https://forum.kx.com/t/how-can-i-apply-a-function-with-if-to-a-table/13305/4 "2024-02-01T00:00:00Z")

</div>

You can also forgo any conditional statements in this case:

&nbsp;

```
q)update r:p1*p2*not p1=neg p2 from t1 
TimeStamp stock p1 p2 r 
------------------------ 
2018.01.01 AAPL 1 -1 0 
2018.01.01 AAPL 2 1 2 
2018.01.02 GOOGL -2 2 0 
2018.01.02 GOOGL 3 0 0
```

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

### Author: ![Ciaran](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@Ciaran](https://forum.kx.com/u/Ciaran)
#### Post date: [February 1, 2024, 12:00am UTC](https://forum.kx.com/t/how-can-i-apply-a-function-with-if-to-a-table/13305/5 "2024-02-01T00:00:00Z")

</div>

Using the vector conditional or applying a function to the columns are both valid solutions that work.  
The vector conditional would be the best approach as it is more efficient to use. Due to the fact that it is doing the calculation within the query and doesn’t have to do an each on each value to get the entry for the new column.

As shown below using a table of 1 million rows, the difference can be seen for either approach

&nbsp;

&nbsp;

&nbsp;
