寻找R中两个数据框之间的差异

时间:2019-03-01 17:37:44

标签: r

我有两个数据框,这是一个示例。我的实际文件有600多行。

Bay = c(1,1,2,3,4,5)
Row = c(1,1,2,3,1,1)
Facings = c(1,2,1,3,1,1)
Product = c("Wipes", "Scented Wipes", "Bleach", "Cleaner", "Dish Soap", "Plunger")
FirstAisle = data.frame(Bay,Row,Facings,Product)
Product = c("Scented Wipes", "Bleach", "Dish Soap", "Wipes", "Cleaner", "Plunger", "Drainer")
Row = c(1,2,2,1,1,1,2)
Bay = c(1,2,2,1,1,5,1)
Facings = c(1,2,1,1,1,2,1)
SecondAisle = data.frame(Bay,Row,Facings,Product)

两者的产品名称相同,但顺序可能不同。在第二条走道中,它们也可能被移到另一排或海湾。他们在第二个走道上也可能有不同数量的饰面。我正在尝试找出一种方法来确定哪些产品已转移到不同区域,以及该产品的饰面数量是否已更改。

1 个答案:

答案 0 :(得分:0)

nm = colnames(FirstAisle)
data.frame(lapply(nm, function(x){
    if(is.numeric(FirstAisle[[x]]) & is.numeric(SecondAisle[[x]])){
        FirstAisle[x] - SecondAisle[x]
    }else{
        data.frame(FirstAisle = FirstAisle[[x]],
                   SecondAisle = SecondAisle[[x]])
    }
}))
#  Bay Row Facings    FirstAisle   SecondAisle
#1   0   0       0         Wipes Scented Wipes
#2  -1  -1       0 Scented Wipes        Bleach
#3   0   0       0        Bleach     Dish Soap
#4   2   2       2       Cleaner         Wipes
#5   3   0       0     Dish Soap       Cleaner
#6   0   0      -1       Plunger       Plunger