如何在R中检查两个文件

用户330

我有两个文件,我想确保这两个文件相同。

文件1:

df1<-read.table (text=" class   type    colour  gender
12  1   yellow  F
11  1   green   M
14  2   red M
18  2   red F
16  1   red F


", header=TRUE)

档案2:

df2<-read.table (text=" class   type    colour  gender
12  1   yellow  F
11  2   gree    M
14  2   red N
18  2   red F
18  1   red F


", header=TRUE)

正如我们在df2中看到的那样,列中有四个错误。例如,在df2中,该类应像在df1中那样读为16,该类为16(最后一行),而不是18。如果df1和df2中的值不相等,我想得到一个FALSE,然后查看错误数量结果是:并且错误= 4。我有近100列,所以它们只是数据的一小部分

out<-read.table (text=" class   type    colour  gender
12  1   yellow  F
11  FALSE   FALSE   M
14  2   red FALSE
18  2   red F
FALSE   1   red F

", header=TRUE)

Error =4
尼洛克

调整丹尼尔的答案

chck <- mapply(function(x,y){
  x[x != y] <- 'FALSE'
  x
}, df1, df2)

chck <- data.frame(chck)

#-------
  class  type colour gender
1    12     1 yellow      F
2    11 FALSE  FALSE      M
3    14     2    red  FALSE
4    18     2    red      F
5 FALSE     1    red      F
sum(!chck)
# 4

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章