0

I have 2 data frames of unequal size and I want to map values from the smaller data frame to the larger one based on the "color" column.

df1:

Color Value
Red A
Blue B
Green C

df2:

Color Repetition
Red 1
Green 1
Red 2
Blue 1
Blue 2
... ...

Desired output:

Color Repetition Value
Red 1 A
Green 1 C
Red 2 A
Blue 1 B
Blue 2 B
... ... ...

Reproducible code:

df1 <- data.frame(c("Red", "Blue", "Green"),
                  c("A", "B", "C"))
names(df1) <- c("Color", "Value")

df2 <- data.frame(c("Red", "Green", "Red", "Blue", "Blue"),
                  c(1,1,2,1,2))
names(df2) <- c("Color", "Repetition")

I've tried merge but it didn't work for me probably because they are of different sizes. I've also tried googling for this problem but have only found python solutions.

Osidaksjdm W
  • 11
  • 1
  • 2
  • Thats funny. Im trying to do this with Python and none of the solutions I am finding are working. – Dan Aug 13 '23 at 23:45

1 Answers1

0

Another satisfactory answer could be found here.

merge(df2, df1, by = "Color", all.x=TRUE)

 Color Repetition Value
1  Blue          1     B
2  Blue          2     B
3 Green          1     C
4   Red          1     A
5   Red          2     A
Rfanatic
  • 2,224
  • 1
  • 5
  • 21