koteletje
koteletje

Reputation: 679

Join datatables using more than one column name stored in one variable

I am trying to perform an update join of two data tables with the fields (more than one) I need to use to join stored in a variable. Below is an example:

library(data.table)
DT1 <- data.table(col1 = 1:5, col2 = 5:1, lett = letters[1:5])
DT2 <- data.table(col1 = c(1:3, 2:5, 1), col2 = c(5:3, 4:1, 5))

joinFields <- c('col1', 'col2')

I tried doing it this way:

DT1[DT2,
    on=c(paste0(joinFields, '=', joinFields)),
    nomatch=0L]

This way is based on a solution suggested in Join datatables using column names stored in variables.

dt1[dt2_temp, 
    on=c(paste0(varName, ">valueMin"), paste0(varName, "<=valueMax")),
    nomatch=0L]

It does not work. Obviously, my case is a bit different, because in the example I used, there are 2 pastes. Is there a solution that continues to allow me using on = c()?

Edit: I am aware I can do it with merge()`

Upvotes: 1

Views: 95

Answers (1)

Carles
Carles

Reputation: 2829

I think you just need to put two ==, as following:

DT1[DT2,
    on=c(paste0(joinFields, '==', joinFields)),
    nomatch=0L]
# col1 col2 lett
# 1:    1    5    a
# 2:    2    4    b
# 3:    3    3    c
# 4:    2    4    b
# 5:    3    3    c
# 6:    4    2    d
# 7:    5    1    e
# 8:    1    5    a

Even you do not need to use c() :

 DT1[DT2,
        on=paste0(joinFields, '==', joinFields),
        nomatch=0L]
    # col1 col2 lett
    # 1:    1    5    a
    # 2:    2    4    b
    # 3:    3    3    c
    # 4:    2    4    b
    # 5:    3    3    c
    # 6:    4    2    d
    # 7:    5    1    e
    # 8:    1    5    a

Upvotes: 4

Related Questions