Q:

Is it possible to pass column name within data.table on argument in the form of a variable in R

I'm trying to perform a non-equi join on data.table as follows:
A <- data.table(p = c(10, 20, 30), q = c(15, 25, 35), r = c(0, 0, 0))
B <- data.table(x = c(5, 15), y = c(15, 25), z = c(1, 2))

This works:
A[B, on = .(p >= x, p < y), r := r + z]

This doesn't work:
col_var <- "p"
A[B, on = .(col_var >= x, col_var < y), r := r + z]
or
A[B, on = .(.(col_var) >= x, .(col_var) < y), r := r + z]

I'm trying to achieve something like this:
col_var <- c("p", "q")
for(i in col_var)
{
  A[B, on = .(.(i) >= x, .(i) < y), r := r + z]
}

I'm able to get the result when I directly use p instead of col_var but I need to use for loop which requires me to store column name in the variable. Is there a way I can use column name stored within a variable?
PS: I tried using .(col_var) but I'm getting an error "argument specifying columns specify non existing column(s)"

A:

You can use strings in the on argument.  This allows us to construct the join using paste:
A[B, on = paste(col_var, c(">= x", "< y")), r := r + z]

