r - Update subset of data.table based on join -
i have 2 data tables, dt1 , dt2:
set.seed(1) dt1<-data.table(id1=rep(1:3,2),id2=sample(letters,6), v1=rnorm(6), key="id2") dt1 ## id1 id2 v1 ## 1: 2 e 0.7383247 ## 2: 1 g 1.5952808 ## 3: 2 j 0.3295078 ## 4: 3 n -0.8204684 ## 5: 3 s 0.5757814 ## 6: 1 u 0.4874291 dt2<-data.table(id2=c("n","u"), v1=0, key="id2") dt2 ## id2 v1 ## 1: n 0 ## 2: u 0
i update dt1 based on join dt2, subset of dt1. example, dt1[id1==3]
, expect value of v1 in row 4 updated in following result:
dt1 ## id1 id2 v1 ## 1: 2 e 0.7383247 ## 2: 1 g 1.5952808 ## 3: 2 j 0.3295078 ## 4: 3 n 0 ## 5: 3 s 0.5757814 ## 6: 1 u 0.4874291
i know how update table (using :=
assignment operator), how join tables (dt1[dt2]
), , how subset table (dt1[id1==3]
). i'm not sure how 3 @ once.
edit: note original example attempts update 1 column, actual data requires updating many columns. consider additional scenarios in dt1b , dt2b:
set.seed(2) dt1b<-dt1[,v2:=rnorm(6)] # copy dt1 , add new column setkey(dt1b,id2) dt1b ## id1 id2 v1 v2 ## 1: 2 e 0.7383247 -0.89691455 ## 2: 1 g 1.5952808 0.18484918 ## 3: 2 j 0.3295078 1.58784533 ## 4: 3 n -0.8204684 -1.13037567 ## 5: 3 s 0.5757814 -0.08025176 ## 6: 1 u 0.4874291 0.13242028 dt2b<-rbindlist(list(dt2,data.table(id2="e",v1=0))) # copy dt2 , add new row dt2b[,v2:=-1] # add new column dt2b setkey(dt2b,id2) dt2b ## id2 v1 v2 ## 1: e 0 -1 ## 2: n 0 -1 ## 3: u 0 -1
based on helpful answers @nmel , @bluemagister, came solution updated scenario:
dt1b[dt2b[dt1b[id1 %in% c(1,2)],nomatch=0],c("v1","v2"):=list(i.v1,i.v2)] dt1b ## id1 id2 v1 v2 ## 1: 2 e 0.0000000 -1.00000000 ## 2: 1 g 1.5952808 0.18484918 ## 3: 2 j 0.3295078 1.58784533 ## 4: 3 n -0.8204684 -1.13037567 ## 5: 3 s 0.5757814 -0.08025176 ## 6: 1 u 0.0000000 -1.00000000
the easiest way can think of key id1
well. eg
setkey(dt1, id2,id1) dt2[, id1 := 3] setkey(dt2, id2, id1) # use i.v1 reference v1 component dt1[dt2, v1 := i.v1 ] dt1 id1 id2 v1 1: 2 e 0.7383247 2: 1 g 1.5952808 3: 2 j 0.3295078 4: 3 n 0.0000000 5: 3 s 0.5757814 6: 1 u 0.4874291
Comments
Post a Comment