Can I access repeated column names in j in data.table union?

If I try to combine two data.tables that have the same column names, then .1 added to one of the names, but I seem to be unable to access the name in part j DT[] expression.

Example:

 DT1 = data.table(name = letters, value = rnorm(26)) DT2 = data.table(name = letters, value = rnorm(26)) setkey(DT1, name) DT1[DT2, value.1 - value] # this doesn't work DT1[DT2][, value.1 - value] # this works 

The motivation for this question was that I thought that one call would be faster, it is not, which leads to a separate question about why: Why DT1 [DT2] [, value1-value] is faster than DT1 [DT2, value1-value] on data.table with fewer columns?

+2
source share
1 answer

You can reference the data.table columns in i , that is, the DT2 columns, with the i prefix as follows:

 DT1[DT2, list(val=i.value-value)] name val 1: a 1 2: b 1 3: c 1 4: d 1 5: e 1 

 # Data used DT1 <- data.table(name=letters[1:5], value=2:6) DT2 <- data.table(name=letters[1:5], value=3:7) setkey(DT1, name) 
+2
source

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


All Articles