2

If I try to join two data.tables that have the same column names, then .1 is appended to one of the names, but I don't seem to be able to access the name in the j part of the 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 the single call would be quicker, this turns out not to be the case, leading to a separate question of why: Why is DT1[DT2][, value1-value] faster than DT1[DT2, value1-value] on data.table with fewer columns?

Community
  • 1
  • 1
Corvus
  • 7,548
  • 9
  • 42
  • 68

1 Answers1

2

You can refer to columns of data.table in i, that is, DT2's columns, with the prefix i 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)
Arun
  • 116,683
  • 26
  • 284
  • 387
  • Great thanks! Any guesses as to why it is slower this way? The original motivation was that I felt splitting into two operation must be inefficient... – Corvus Jul 18 '13 at 09:05
  • 1
    Corone, It seems like two different questions to me. Maybe it's better as another post? It doesn't go with your title (which is the first part) and people who search for your second question will mostly not land on this question.. Better for SO format. – Arun Jul 18 '13 at 09:08
  • Good point, will do that and leave a link behind – Corvus Jul 18 '13 at 09:11
  • Great, I'll prepare the answer to the question :). – Arun Jul 18 '13 at 09:12
  • Question created: http://stackoverflow.com/q/17719480/1900520 – Corvus Jul 18 '13 at 09:24
  • Is there a place where what is available to the expressions in `j` is documented? Is it a full environment, where I can use functions like `get`? – hgcrpd Aug 11 '13 at 15:08