r - Merging different size data frames and repeating values -


i need merge 2 different size data frames. larger 1 (df1) has column several repeated values (licto), shorter 1 (df2) has column licto, values not repeated. df2 has id column. need new column in df1 ids in df2, repeated according repeated values in licto. example below make clearer.

df1<-data.frame(licfrom=c(15470,16307,17121,15350,16982,17182,20319,16727,16946,16262,16605,               16607,15924,15399,15404,16739,16839,16842,16899,16157,15399),         licto=c(17121,17121,17121,16982,16982,16982,16982,16946,16946,16262,16607,             16607,15924,16839,16839,16839,16839,16839,16839,16157,15399)) 

.

df2<-data.frame(licto=c(17121,16982,16946,16607,15924,16839,16157,15399),         fisherid=c(160,760,770,406,106,2196,17323,2441)) 

my data frames this:

df1                 df2  licfrom licto       licto   fisherid 15470   17121       17121   160 16307   17121       16982   760 17121   17121       16946   770 15350   16982       16262   947 16982   16982       16607   406 17182   16982       15924   106 20319   16982       16839   2196 16727   16946       16157   17323 16946   16946       15399   2441 16262   16262            16605   16607            16607   16607            15924   15924            15399   16839            15404   16839            16739   16839            16839   16839            16842   16839            16899   16839            16157   16157            15399   15399    

and final data frame should this:

licfrom licto   fisherid 15470   17121   160 16307   17121   160 17121   17121   160 15350   16982   760 16982   16982   760 17182   16982   760 20319   16982   760 16727   16946   770 16946   16946   770 16262   16262   947 16605   16607   406 16607   16607   406 15924   15924   106 15399   16839   2196 15404   16839   2196 16739   16839   2196 16839   16839   2196 16842   16839   2196 16899   16839   2196 16157   16157   17323 15399   15399   2441 

any appreciated, have spent several hour trying merge need. have used merge , %in% no success. thanks!

you can function merge().

 merge(df1,df2,sort=false)     licto licfrom fisherid 1  17121   15470      160 2  17121   17121      160 3  17121   16307      160 4  16982   15350      760 5  16982   16982      760 6  16982   20319      760 7  16982   17182      760 8  16946   16727      770 9  16946   16946      770 10 16607   16605      406 11 16607   16607      406 12 15924   15924      106 13 16839   15399     2196 14 16839   15404     2196 15 16839   16739     2196 16 16839   16839     2196 17 16839   16842     2196 18 16839   16899     2196 19 16157   16157    17323 20 15399   15399     2441 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -