1

I want to merge two data frames that have the same column name and some similar values. I.e

         Dataframe 1                                  Dataframe 2
    ID    COLOUR   FRUIT                        ID    SHAPE   STATUS 
    A     Yellow   Apple                        A     Circle   On
    B     Blue     Banana                       B     Square   On
    C     Purple   Apple                        D     Triangle Off
    D     Orange   Grapes                           

I want to 1) compare the IDs of these two datagrams and keep the ones that occur in both data frames and remove the ones that don't overlap. 2) Combine the two data frames using the common values of the ID column. I tried to make the ID column into rownames, but since some of the IDs are duplicated in each of these data frames, I was unable to do that.

Any help is appreciated.

Thank you in advance!

2 Answers 2

3
library(data.table)

# set as data.table
lapply(list(df1, df2), \(i) setDT(i))

# inner join
df1[df2, on=.(ID), nomatch=0]
Sign up to request clarification or add additional context in comments.

Comments

2

We are looking for a inner join here:

library(dplyr)

inner_join(`Dataframe 1`, `Datadrame 2`, by = 'ID')

Comments

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.