1

I have two data frames. First has 7 columns and the second has 2 columns. The common column name is "flavor". I want to merge these two data frames but the problem is that the 2nd data frame only contains the subset of 1st data frame in column "flavor". For example,

df1$flavor <- c("mango|grapes|watermelon", "coffee|tea", "beer|alcohol|wine")
df2$flavor <- c("mango", "tea", "wine")

When I am trying to merge the both data frames, I am loosing few rows. So how to merge both data frames without effecting the rows and the columns.

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
Mani
  • 11
  • 1
  • lookup the different between left join, right join, inner join and outer join, then you can use the `merge` function along with all.x=T, all.y=T or all=T to get the one you want – morgan121 Feb 28 '19 at 05:42
  • 1
    Possible duplicate of [How to merge two data frame based on partial string match with R?](https://stackoverflow.com/questions/34720461/how-to-merge-two-data-frame-based-on-partial-string-match-with-r); [dplyr: inner_join with a partial string match](https://stackoverflow.com/questions/32914357/dplyr-inner-join-with-a-partial-string-match) – A. Suliman Feb 28 '19 at 06:06

1 Answers1

0

You can use "fuzzyjoin" as this allows you to match partial strings. Let's say you have df1(7 columns) and df2(2 columns) :

 library(fuzzyjoin)
 df1 %>% regex_inner_join(df2, by = c(flavor = "flavor"))
adjustedR2
  • 161
  • 4