1

I have 'df1' with data that look like this:

Object Thing
apple tini drink
vodka cran beverage
tom collins alcohol
arnie palmer cocktail

And I have 'df2' with data that look like this:

Object1 Thing1
apple tini drink
vodka cran bever
tom collins alc
arnie palmer cocktail

I am looking to check and see if Object and Thing from df1 match any Object1 and Thing1 from df2, and if they do match, a 1 is created in a new column in df1 and if they don't, a 0 is created, so that it looks like this:

Object Thing Value
apple tini drink 1
vodka cran beverage 0
tom collins alc 0
arnie palmer cocktail 1

Note that columns are intentionally different in case they don't match is the actual dataframe.

Thank you!

1 Answer 1

1

Making an extra column and then left_joining solves this:

library(tidyverse)

df3 <- mutate(df2, Value = 1)

left_join(df1, df3, by=c("Object" = "Object1",
                         "Thing" = "Thing1")) %>%
  mutate(Value = replace_na(Value, 0))

#    Object    Thing Value
# 1    tini    drink     1
# 2    cran beverage     0
# 3 collins  alcohol     0
# 4  palmer cocktail     1
Sign up to request clarification or add additional context in comments.

1 Comment

My dataset is quite large, so I could be missing something, but it seems to work just fine. Will update if I see any issues. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.