逐个单元地比较两个数据帧

时间:2017-09-27 06:25:07

标签: r dataframe compare

假设我们有两个数据框如下

this.route('download', {
    where: 'server',
    path: '/download/:_id',
    action: function() {
        destPath="/home/rootuser/botbuilder/botBuilderdevelo/Python.zip"
        if (fs.existsSync(destPath)) {
            filetext = fs.readFileSync(destPath, "utf-8");//tried encoding binary
        }

        var headers = {
            'Content-Type':  'application/octet-stream',//tried application/zip
            'Content-Disposition': "attachment; filename=" + "pyth" + '.zip'
        };
        this.response.writeHead(200, headers);
        return this.response.end(filetext);
        // i tried this.response.end(destpath);
    }   
})

我要比较数据框的每个单元格&#39;一个&#39;到数据帧的每个相应的单元格&#39;两个&#39; (不使用for循环)。如果内容相同,我想得到一个TRUE,如果内容不相同,我想要一个one <- data.frame("a" = c("aa", "bb"), "b" = c("cc", "dd")) two <- data.frame("b" = c("aa", "bb"), "c" = c("ee", "ff")) 。结果FALSE看起来像

dataset

有没有办法在不使用for循环的情况下执行此操作?

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

G&#39;!天

您需要使用identical()

one <- data.frame("a" = c("aa", "bb"), "b" = c("cc", "dd"))

two <- data.frame("b" = c("aa", "bb"), "c" = c("ee", "ff"))

three <- data.frame("a" = c("aa", "bb"), "b" = c("cc", "dd")) ## three is identical to one


identical(one, two) #returns FALSE

identical (one, three) #returns TRUE

干杯!

答案 1 :(得分:0)

尝试以下方法:

result <- data.frame(Col1 = (as.character(one$a)==as.character(two$b)), 
                     Col2 = (as.character(one$b)==as.character(two$c)))

输出:

Col1  Col2
1 TRUE FALSE
2 TRUE FALSE

答案 2 :(得分:0)

我们可以做到

as.matrix(one) == as.matrix(two)

最好在stringsAsFactors= FALSE调用中使用data.frame,以避免将字符元素类转换为factors。转换为matrix后,该类现在为character,然后使用逐个单元格与==进行比较