R data.table j is valid only when not when making a connection

I just dive right into it ...

DT_1 <- data.table(pk = c(1,1,2,2), val = 1:4, key = 'pk')
DT_2 <- data.table(pk = 1:2, att = c('a','b'), key = 'pk')

It works.

> DT_2[i = DT_1, j = `:=`(max_val = max(val))]
> DT_2
   pk att max_val
1:  1   a       4
2:  2   b       4

This is not true.

> DT_2[i = DT_1, j = `:=`(max_val = max(val)), by = .(pk)]
Error in `[.data.table`(DT_2, i = DT_1, j = .(max(val)), by = .(pk)) : 
  object 'val' not found

Why and what should I do about it?

+4
source share

Source: https://habr.com/ru/post/1689343/


All Articles