r - Merge with multiple columns by conditions -


i want merge data condition. browsed many websites did not find want.
have 2 data here:

# dt1 cola1   cola2     colb    colc    cold           area ta43    ti44      s2230   amy     2014-08-08     usa ta63    ti64      t1205   andy    2014-01-01     canada ta28    ti100     l1288   peter   2014-01-08     eu ta28    ti100     l2231   roger   2014-01-08     eu ta92    na        a2206   jean    2014-01-12     china ta14    na        e2240   freda   2014-01-05     japan ta69    ti50      n1029   tina    2014-01-05     mexico  # dt2 cola     colb    colc    cold           type ti64     t1205   andy    2014-01-01     ti100    l1288   peter   2014-01-08     ti100    l2231   roger   2014-01-08     ta92     a2206   jean    2014-01-12      ta14     e2240   freda   2014-01-05     r ta69     n1029   tina    2014-01-05     

what want is:

cola     colb    colc    cold           type   area ti64     t1205   andy    2014-01-01          canada ti100    l1288   peter   2014-01-08          eu ti100    l2231   roger   2014-01-08          eu ta92     a2206   jean    2014-01-12          china ta14     e2240   freda   2014-01-05     r      japan ta69     n1029   tina    2014-01-05          mexico 

i explain here:
want mapping dt1 dt2 cola, colb, colc , cold.
if column type in dt2 a , r, merge cola in dt2 cola1 in dt1.
if column type in dt2 i, merge cola in dt2 cola2 in dt1.

any idea data.table way?

this gets desired output dplyr. may use inner_join or right_join depending on you're trying achieve:

library(dplyr) library(tidyr) dt2 %>% mutate(merge_col = ifelse(type == "i","cola2","cola1")) %>%   left_join(dt1 %>% gather(merge_col,cola,cola1,cola2))  # joining, = c("cola", "colb", "colc", "merge_col") # cola  colb  colc       cold type merge_col   area # 1  ti64 t1205  andy 2014-01-01        cola2 canada # 2 ti100 l1288 peter 2014-01-08        cola2     eu # 3 ti100 l2231 roger 2014-01-08        cola2     eu # 4  ta92 a2206  jean 2014-01-12        cola1  china # 5  ta14 e2240 freda 2014-01-05    r     cola1  japan # 6  ta69 n1029  tina 2014-01-05        cola1 mexico 

data.table

with data.table can try this, it's exact translation:

merge(   dt2[,merge_col := ifelse(dt2$type == "i","cola2","cola1")],   melt(dt1,id = c("colb","colc","cold","area"),measure=c("cola1","cola2"),"merge_col","cola"),   all.x = true ) 

adjust parameters all.x , all.y depending on type of join want


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -