R

时间:2017-08-27 03:04:58

标签: r dataframe merge conditional match

我有两个行数不等的数据框:

df1

Label   Country    Year
NU      Germany    1999
BU      Italy      1945
LU      France     2009
KM      Sweden     1998
NU      Germany    1999

df2

Make     Label     
X1       NU
X2       BU
X3       NU
X4       NU
X5       KM

我想做的是匹配"标签"并最终得到以下结论:

Make   Label    Country     Year
X1       NU     Germany     1999
X2       BU     Italy       1945
X3       NU     Germany     1999
X4       NU     Germany     1999
X5       KM     Sweden      1998

1 个答案:

答案 0 :(得分:0)

 a=merge(df2,df1,by="Label")
 a[!duplicated(a),]
   Label Make Country Year
 1    BU   X2   Italy 1945
 2    KM   X5  Sweden 1998
 3    NU   X1 Germany 1999
 5    NU   X3 Germany 1999
 7    NU   X4 Germany 1999