2

I have two dataframes df1 and df2. df1 has column "ID" while df2 has column "ID_NUMBER".

Here's df1 in comma separated format:

ID, ANIMAL
1, dog
2, alligator
4, cat
5, cougar

And df2

ID_NUMBER, WEIGHT
1, 100
2, 1000
3, 12
5, 125

How do I merge these two datasets on df1$ID/df2$ID_NUMBER, keeping only the rows where they have the ID number in common?

Username
  • 3,463
  • 11
  • 68
  • 111

1 Answers1

2

Here is the output. Is this not what you want?

df1 <- read.table(text = 'ID, ANIMAL
                          1, dog
                          2, alligator
                          4, cat
                          5, cougar', header = TRUE)
df1
df2 <- read.table(text = 'ID_NUMBER, WEIGHT
                          1, 100
                          2, 1000
                          3, 12
                          5, 125', header = TRUE)
df2
merge(df1, df2, by.x = 'ID.', by.y = 'ID_NUMBER.')
  ID.    ANIMAL WEIGHT
1  1,       dog    100
2  2, alligator   1000
3  5,    cougar    125
Gopala
  • 10,363
  • 7
  • 45
  • 77