IRT
IRT

Reputation: 209

Keep columns in join data.table

I do not get why in this join I can not retrieve the column sub_item of my DT2?

DT <- data.table(ID=c(1:4),OBS_VALUE=10:13)
DT2 <- data.table(ID=c(1:4),sum_item=c(10,11.5,12.5,18))

setkey(DT,ID)
setkey(DT2,ID)

S_toset_sum <- DT[DT2,diff := abs(OBS_VALUE-sum_item)][diff<3]

in the output I would like to have still sum_item as I want to keep this column instead of the OBS_VALUE column.

Upvotes: 1

Views: 2217

Answers (1)

pyll
pyll

Reputation: 1764

You have to specify the columns you wish to keep, as well as the key you wish to join on.

S_toset_sum <- DT[DT2, on = 'ID', .(ID, OBS_VALUE, sum_item, diff = abs(OBS_VALUE-sum_item))][diff<3]

Upvotes: 1

Related Questions