2016-03-25 33 views
0

我在R中有两个数据框,其中都有一个名为“typeid”的列。如何匹配两个R表中链接ID的表之间的值

数据框中一个看起来是这样的:

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

数据帧B相类似这样的:

Typeid Color 
1  Red 
2  Blue 
3  Green 

有没有办法从东风b添加相应的颜色值的df,A,根据匹配他们键入?我希望最终产品看起来像这样:

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

有什么想法吗?谢谢!

回答

2

我们可以使用merge

merge(A, B) 

match

A$Color <- B$Color[match(A$Typeid, B$Typeid)] 
+1

该诀窍,谢谢! – BioBaker

相关问题