mayool
mayool

Reputation: 148

Joining two datasets in R

I got 2 Dataset that I want to combine

Dataset_1:

id| value_1
1 |   a
1 |   b
1 |   b
2 |   a
2 |   a
2 |   b
...

Dataset_2:

id| value_2
1 |   123
1 |   433
1 |   234
2 |   222
2 |   333
2 |   333
...

and the result should look like:

id| value_1 | value 2
1 |   a     | 123
1 |   b     | 433
1 |   b     | 234
2 |   a     | 222
2 |   a     | 333
2 |   b     | 333

if tried to use these functions:

inner_join(dataset_1,dataset_2,by="id")
and
full_join(dataset_1,dataset_2,by="id") 
and
merge(dataset_1,dataset_2,by="id")

but i always get all possible combinations of the 2 datasets and not the combined one. It should be simple but I can't figure out what I am doing wrong.

id is a double, value_1 is a chr and value_2 is an int.

Thanks for any help!

Upvotes: 0

Views: 167

Answers (1)

André Oliveira
André Oliveira

Reputation: 175

Your example displays the need for a bind not a join.

Dataset_3 <- bind_cols(Dataset_1,Dataset_2[-1]  )

What happening is:

When a join finds a repeated id, it creates more cases for each combination of results.

Upvotes: 2

Related Questions