0

The following example best speaks for itself.

I would like to get the "desired" outcome.

Any efficient R way to do it is welcome.

df1 = data.frame(id = c(1:6), feature1 = c("class1", "class2", "class3", "class4", "class5", "class6"))
df2 = data.frame(id = c(2, 2, 2, 4, 6), feature2 = c("a", "b", "c", "d", "e"))

desired <- data.frame(id = c(2, 2, 2, 4, 6), feature2 = c("a", "b", "c", "d", "e"), feature1 = c(rep("class2", 3), "class4", "class6"))

> df1
  id feature1
1  1   class1
2  2   class2
3  3   class3
4  4   class4
5  5   class5
6  6   class6
> df2
  id feature2
1  2        a
2  2        b
3  2        c
4  4        d
5  6        e
> desired
  id feature2 feature1
1  2        a   class2
2  2        b   class2
3  2        c   class2
4  4        d   class4
5  6        e   class6
ODstuck
  • 189
  • 1
  • 7
  • https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right – Edo Oct 01 '20 at 16:56
  • 3
    Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – Edo Oct 01 '20 at 16:57
  • Yeah, cloudy perception here, thanks – ODstuck Oct 01 '20 at 19:12

1 Answers1

0

Does this work?

> merge(df2, df1)
  id feature2 feature1
1  2        a   class2
2  2        b   class2
3  2        c   class2
4  4        d   class4
5  6        e   class6


> df2 %>% inner_join(df1)
Joining, by = "id"
  id feature2 feature1
1  2        a   class2
2  2        b   class2
3  2        c   class2
4  4        d   class4
5  6        e   class6
> 
Karthik S
  • 11,348
  • 2
  • 11
  • 25