I have two data.table X and Y.
columns in X: area, id, value
columns in Y: ID, price, sales
Create the two data.tables:
X = data.table(area=c('US', 'UK', 'EU'),
id=c('c001', 'c002', 'c003'),
value=c(100, 200, 300)
)
Y = data.table(ID=c('c001', 'c002', 'c003'),
price=c(500, 200, 400),
sales=c(20, 30, 15)
)
And I set keys for X and Y:
setkey(X, id)
setkey(Y, ID)
Now I try to join X and Y by id
in X and ID
in Y:
merge(X, Y)
merge(X, Y, by=c('id', 'ID'))
merge(X, Y, by.x='id', by.y='ID')
All raised error saying that column names in the by
argument invalid.
I referred to the manual of data.table and found the merge
function not supporting by.x
and by.y
arguments.
How could I join two data.tables by different column names without changing the column names?
Append:
I managed to join the two tables by X[Y]
, but why merge
function fails in data.table?
See Question&Answers more detail:
os