Combine two data tables depending on column conditions

I was wondering how combining two data.tables based on column conditions works! So which column is used, internal or / before external?

require(data.table)

outer <- data.table(KeyColumn=letters, value_outer=seq_along(letters))
inner  <- data.table(KeyColumn=letters[4:6], value_inner=c(100,101,102))

setkey(outer, KeyColumn)
setkey(inner, KeyColumn)

outer[inner] # works as expected

inner[value_outer<10] # error as expected, because column doesn't exist in inner

outer[inner[value_outer<10], NewColumn := value_inner] # why does this work?

If a conditional column is present (the names are identical) in both data.tables, but the values ​​differ, which column is used, internal or / before external?

+4
source share
1 answer

Thanks to @eddi who responded in a comment:

innerlooks at its columns, doesn’t find value_outer, therefore, looks at its parental environment, which outer, therefore, value_outercoincides withouter$value_outer

+1
source

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


All Articles