Question: I have collection in which consist of large amount of **Hetrogenous Objects**. e.g.

```
    Employee e1, e2, e3 = new Employee();
    Student s1, s2, s3 = new Student();
    Department d1, d2, d3 = new Department(); 
........etc 

```

I need to traverse this collection and all similar objects needs to grouped together and put in another new collection. Means `e1, e2, e3` we can put in one collection.
We can use `InstanceOf`, but that will good if there are only 2-3 types of object.
What will generic solution for this.

Answer: 
[Left join](https://en.wikipedia.org/wiki/Join_(SQL)#Left_outer_join) then update:

```
library(dplyr)

left_join(t1, t2, by = c("x1", "x2"), all.x = TRUE) %>% 
  mutate(x3 = ifelse(is.na(x3.y), as.character(x3.x), as.character(x3.y))) %>% 
  select(-c(x3.x, x3.y))

#   x1 x2 x3
# 1  1  I  a
# 2  7  R  a
# 3  3  R  a
# 4  4  I  b
# 5  2  I  b
# 6  6  R  b

```

Using base R:

```
# left join 
res <- merge(t1, t2, by = c("x1", "x2"), all.x = TRUE)
# update x3
res$x3 <- ifelse(is.na(res$x3.y), as.character(res$x3.x), as.character(res$x3.y))
# subset and reorder
res <- res[match(t1$x1, res$x1), c("x1", "x2", "x3")]

```
I think this would give what you want. 

Convert `x3` column to character in `t1` and `t2`and then check the condition

```
t1$x3 <- as.character(t1$x3)
t2$x3 <- as.character(t2$x3)
t1[t1$x1 %in% t2$x1 & as.character(t1$x2) %in% as.character(t2$x2), "x3"] <- t2$x3

> t1
#  x1 x2 x3
#1  1  I  a
#2  7  R  a
#3  3  R  a
#4  4  I  b
#5  2  I  b
#6  6  R  b

```