r - Merge for multiple columns and rows -
given following data tables:
> d1 <- data.table(letter=c("a", "b", "a", "b"), v1=c(0,0,0,0),v2=c(0,0,0,0)) > d2 <- data.table(letter=c("a", "b"), value=c(1,2)) > d1 letter v1 v2 1: 0 0 2: b 0 0 3: 0 0 4: b 0 0 > d2 letter value 1: 1 2: b 2
i seek fill columns v1 , v2 d1 using values d2, that:
> d12 letter v1 v2 1: 1 1 2: b 2 2 3: 1 1 4: b 2 2
i assume have use function 'merge' somehow have not managed.
any advice on how achieve this?
many in advance!
setkey(d1, letter) setkey(d2, letter) d1[d2, c('v1','v2'):=value] # d1 holds updated dt
the d1[d2]
merges d1 d2; c('v1', 'v2'):=value
assigns value
column v1 , v2. rows in d1
have letters not in d2
left as-is.
Comments
Post a Comment