[datatable-help] Is there a way to merge-assign on something other than the key?

Steve Lianoglou lianoglou.steve at gene.com
Thu Aug 15 18:23:53 CEST 2013


Hi Frank,

I don't have time to get into the details of your entire question
right now, but:

On Thu, Aug 15, 2013 at 8:49 AM, Frank Erickson <FErickson at psu.edu> wrote:
> Hi,
>
> I really like the DT1[DT2,z:=...] idiom. Unfortunately, the value of a
> merge() on other columns is a new data.table, so modifying DT1, like
> merge(DT1,DT2,by=...)[,z:=...], is not possible. Or is there actually a way
> to do this that I am missing?

I just wanted to mention that unless I am misunderstanding what you
want, this is entirely possible, and the way you suggest it might work
is actually the way to do it.

Consider:

R> dt1 <- data.table(a=sample(letters[1:2], 5, rep=T), b=runif(5), key='a')
R> dt2 <- data.table(a=c('a', 'b'), c=rnorm(5), key='a')

R> dt1
   a          b
1: a 0.02517147
2: a 0.85459776
3: a 0.67472168
4: a 0.89684769
5: b 0.11619613

R> dt2
   a           c
1: a -0.07817539
2: b -1.28897689

R> out <- merge(dt1, dt2)[, d := b + c]
   a          b           c           d
1: a 0.02517147 -0.07817539 -0.05300392
2: a 0.85459776 -0.07817539  0.77642237
3: a 0.67472168 -0.07817539  0.59654629
4: a 0.89684769 -0.07817539  0.81867230
5: b 0.11619613 -1.28897689 -1.17278075

Will come back later to look through the rest of your question if
still necessary.

HTH,
-steve

-- 
Steve Lianoglou
Computational Biologist
Bioinformatics and Computational Biology
Genentech


More information about the datatable-help mailing list