0

I have two dataframes in R, where both have a column called "typeid".

Dataframe A looks like this:

Shirt  Typeid
1      2
2      2
3      1
4      3
5      1

Dataframe B looks like this:

Typeid  Color
1       Red
2       Blue
3       Green

Is there a way to add the corresponding Color values from df B to df A, matching them according to typeid? I'd want the end product to look like this:

Shirt  Typeid   Color
1      2        Blue
2      2        Blue
3      1        Red
4      3        Green
5      1        Red

any ideas? Thanks!

joran
  • 163,977
  • 32
  • 423
  • 453
BioBaker
  • 13
  • 4

1 Answers1

2

We can use merge

merge(A, B)

or match

A$Color <- B$Color[match(A$Typeid, B$Typeid)]
akrun
  • 789,025
  • 32
  • 460
  • 575