Question Details

No question body available.

Tags

r data.table

Answers (2)

Accepted Answer Available
Accepted Answer
June 21, 2025 Score: 1 Rep: 167,708 Quality: High Completeness: 60%

This is a double join, the first with id == apple, the second with id == pear, measure == measure. (And then a rename and optional column reordering.)

dt1[dt2, on = .(id == apple), nomatch = NULL] |>
  dt1[i = , on = .(id == pear, measure == measure)] |>
  setnames(c("id", "i.id", "value", "i.value"),
           c("pear", "apple", "pear.val", "apple.val")) |>
  [, c("type", "i.type") := NULL ]

pear measure pear.val apple apple.val

1: d 1 1 a 1

2: d 2 8 a 5

3: d 1 1 b 1

4: d 2 8 b 9

5: f 1 9 c 4

6: f 2 5 c 2

The order of the columns is different but it's the same data.

June 21, 2025 Score: 0 Rep: 23 Quality: Low Completeness: 60%

I've come up with a fairly inelegant solution to achieve the double join (thanks to r2evans for the terminology and the point in the right direction!):

# Step 1: split dt1 into apple and pear tables apple_dt