处理两个具有公共字段

时间:2015-10-14 10:33:38

标签: r merge dataframe

我有两个数据集。一个只有注册号(A),另一个注册号列表和注册号(B)旁边的型号类型。

A

A

ircraft.reg.num Action  Description Service.Type    C/O Order.Type  Created.on  Actual.release.date
5Y-BX   Re-Assembling of Aircraft           C/O ZS08    40512   40532
5Y-BX   Re-Assembling of Aircraft           C/O ZS08    40512   40532
5Y-BX   Re-Assembling of Aircraft           C/O ZS08    40512   40532
5Y-BX       Dual Control Collective Lever           ZS08    40497   40550
5Y-BX       Dual Control Collective Lever           ZS08    40497   40550
5Y-BX   Perform SB  63-10 MGB plug          ZS08    40548   40550
5Y-BX   Perform SB  63-10 MGB plug          ZS08    40548   40550
5Y-BX   Defect  Pitot static cover burnt            ZS08    40497   40550
5Y-CD   Airworthiness Inspection & Test             ZS08    40711   40711
5Y-CD   Airworthiness Inspection & Test             ZS08    40711   40711
5Y-CD   Airworthiness Inspection & Test             ZS08    40711   40711

Reg.    Aircraft.Model
5O-MV   AS 355 
5O-Unknown  SA 318 
5O-Unknown  SA 318 
5T-Unlnown  SA 318
5T-BF   BO 105 
5T-BW   AS 350 
5T-BX   AS 350 
5T-BY   AS 350 
5T-CA   BK 117 
5T-CC   AS 350 
5T-CD   AS 350 
5T-DK   AS 350 
5T-DS   AS 350 
5T-DS   AS 350 

我想知道A中的注册号是否等于B中的注册号,模型的类型是什么?

我目前使用:

if(A$Aircraft.reg.num -> B$Reg.){
model <- reg.no$Aircraft.Model
}

但它不起作用,因为我的data.frames甚至不长。

有关如何在不使用&#34; if&#34;的情况下获得结果的任何建议功能

谢谢, 卡门

2 个答案:

答案 0 :(得分:0)

你可以在plyr包中使用join函数作为excel中的一种VLOOKUP。

library(plyr)
newdf <- join(A,B[,1], by = "aircraft.reg.num", type = "left join")

上面的代码如果存在则应该给你相同的id号,如果不存在则给NA。您可以在下面的链接中找到更多示例,并提供类似的问题。

How to do vlookup and fill down (like in Excel) in R?

答案 1 :(得分:0)

也可以使用sqldf包。它允许对数据帧进行SQL样式查询。

library(sqldf)
df <- sqldf('SELECT A."aircraft.reg.num", B."Aircraft.Model"
              FROM A INNER JOIN B ON A."aircraft.reg.num" = B."Reg."')

如果您想查看B中缺少哪些飞机注册,您也可以使用LEFT JOIN

注意:包含句点的列名必须用“...”或[...]包围,因为句点是SQL运算符。