Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

How to check two files in R

I have two files and I want to make sure these two files are the same.

File 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)

File 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)

As we can see in df2, there are four errors in columns. For example in df2, the class should read 16 as in df1, the class is 16 (last row), not 18. If the values in df1 and df2 are not equal, I want to get a FALSE and then see the number of errors. the outcome is: and the error=4. I have nearly 100 columns and so they are just a small sample of the data

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A tweak to Daniel's answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...