Reputation: 11
I'm really new to r. But I was wondering if it is possible to compare two data frames. For example, I have a data frame with a column containing the values "peanut_butter", "applesauce", "apple juice" and another data frame column containing the value "apple", "peanut". Is there a way for me to make a loop such that for every row in the first column, it checks against each row of the second column and if there's a partial match (peanut butter contains peanut) it would create a new column with the name of the match?
Upvotes: 1
Views: 322
Reputation: 78947
We could use fuzzyjoin
:
It has two functions: regex_inner_join
and fuzzy_inner_join
for partial matching strings:
library(fuzzyjoin)
df1 %>%
regex_inner_join(df2, by = "col1")
col1.x col1.y
<chr> <chr>
1 peanut_butter peanut
2 applesauce apple
3 apple juice apple
data:
library(tibble)
df1 <- tribble(
~col1,
"peanut"
"peanut_butter",
"applesauce",
"apple juice"
)
df2 <- tribble(
~col1,
"apple",
"peanut"
)
Upvotes: 2