bumblebee
bumblebee

Reputation: 1116

amend table by data.table join

I have two tables

library(data.table)
X <- data.table(id=1:4, a=1, b=2, c=3, d=4, e=5, f=6)
I <- data.table(id=1:3, g=7, z=26)

and I would like to inner-join one column of I into X without specifying that I would like to keep a to f.

If I were to do a left-join, I could simply

X[I, g:=i.g, on="id"]

without specifying that a to f should be kept. I look for something similar for an inner join.

Upvotes: 0

Views: 71

Answers (3)

pseudospin
pseudospin

Reputation: 2777

I think you mean this

X[I[, .(id, g)], on='id']

Upvotes: 1

Humpelstielzchen
Humpelstielzchen

Reputation: 6441

You could filter it out afterwards:


I[X, on = "id", nomatch = 0,][,!"z"]

   id g a b c d e f
1:  1 7 1 2 3 4 5 6
2:  2 7 1 2 3 4 5 6
3:  3 7 1 2 3 4 5 6

Upvotes: 0

s_baldur
s_baldur

Reputation: 33548

look for something similar for an inner join?

Inner join can be done with:

X[I, on = "id", nomatch = 0] 

Or

I[X, on = "id", nomatch = 0] 

Upvotes: 0

Related Questions