Christoph P. Christoph P. - 1 month ago 17
R Question

Merging numeric and characters from different datasets

I am trying to combine two datasets.
Dataset 1 has ca. 4000 rows and Dataset 2 has 132 rows. I want to match the

Brand
names in Dataset 2 with the
UPS
one in dataset 1. So All the
UPS
have the corresponding
Brands
in 1 as well. I tried to merge them both with
merge
. However, I have been so far unsuccessful of merging them.

DataSet 01:
Where UPS is numeric

UPS WEEK AP
1 1111112016 1 385.22
2 1111112016 2 221.63
3 1111112016 3 317.47
4 1111112016 4 173.71
5 1111112016 5 269.55


Dataset 02:

UPC Brand
1 1111112016 Dove
2 1111112440 Dove
3 1111112480 Dove
4 1111112501 Dove
5 1111132008 Lever
6 1111132012 Lever
7 1111132048 Lever
8 1111132122 Lever


This is how i tried it so far:

Brand = c(unique(UB$Brand))
UPS = c(unique(PAW2$UPS))
PAWn = merge(PAW, UB, by.x = "UPS", by.y = "Brand")


I am aware that there are other posts out there. But so far they did not help.

Answer

Based on your description, I think you need:

 merge(PAW, UB, by.x = "UPS", by.y = "UPC", all.x = TRUE)

to get what you want. As Nicola already said in the comments, the only way you can match the Brand names in UB to the UPS codes in PAW is via matching with the UPC codes in UB.

This also works when UB$UPC is a character variable and PAW$UPS is a numeric variable.

By adding all.x = TRUE all observations in PAW are returned, even when they don't have matching values in UB.