[R] Merging two dataframes with a condition involving variables of both dataframes

Collin Lynch collinl at cs.pitt.edu
Fri Nov 8 03:37:29 CET 2013

You might need to implement it as a nested pair of for loops using rbind.
In essence iterate over the rows in df1 and each time find the matching
row in df2.  If none is found then add the df1 row by itself to the
result.  If one is then remove it from df2 and rbind both of them.  Once
done just merge in all rows that remain in df2.

This would likely be slower than a sql-based method but is essentially the
same algorithm.

You can find advice on for-loops in R here:


More information about the R-help mailing list