Short answer: yes, see the accepted reply.

---

I have the two `data.table` below.

```
stocks = data.table(Ticker = c('xx','xx','yy','yy'), Date = c(as.IDate("2000-01-01"), as.IDate("2000-01-02")), t = c(1.8, 3.5))
   Ticker       Date   t
1:     xx 2000-01-01 1.8
2:     xx 2000-01-02 3.5
3:     yy 2000-01-01 1.8
4:     yy 2000-01-02 3.5
tt = data.table(Date = c(as.IDate("2000-01-01"), as.IDate("2000-01-02")), t0 = c(1,2), t1 = c(2,3), t2 = c(3,4), y0 = c(10, 20), y1 = c(-20, -30), y2 = c(33,44))
         Date t0 t1 t2 y0  y1 y2
1: 2000-01-01  1  2  3 10 -20 33
2: 2000-01-02  2  3  4 20 -30 44

```

For each row in `stocks`, I want to find the approximate `y` given `t`, based on linear interpolation of values in `tt`.

```
zz = tt[stocks, on = 'Date']
zz[, y.approx := approx(c(t0,t1,t2), c(y0,y1,y2), t)$y, by = 'Date,Ticker']
         Date t0 t1 t2 y0  y1 y2 Ticker   t y.approx
1: 2000-01-01  1  2  3 10 -20 33     xx 1.8      -14
2: 2000-01-02  2  3  4 20 -30 44     xx 3.5        7
3: 2000-01-01  1  2  3 10 -20 33     yy 1.8      -14
4: 2000-01-02  2  3  4 20 -30 44     yy 3.5        7

```

The problem is that doing this way has lots of duplicate calculation. Ideally I want to define an `approxfun` for each day and apply it to each row in `stocks`. But datatable cannot take function objects as its element.

```
tt[, ff := approxfun(c(t0,t1,t2), c(y0,y1,y2)), by = Date]
Error in `[.data.table`(tt, , `:=`(ff, approxfun(c(t0, t1, t2), c(y0,  : 
  j evaluates to type 'closure'. Must evaluate to atomic vector or list.

```

My question is:

1. Is there a better way than doing `approx` on each row (and being slow)?
2. Is it possible for datatable to have function objects as its element?