Compare data between two columns

873 views Asked by At

I have two data sets. After merging them by a given ID, i want to compare two variables and report the ID or position where the values aren't equal. i.e. check whether value df$b[n]== df$b[n]

I have tried using comparebelow but it reports FALSE or TRUE

 x.1 <- data.frame(id=c(1,2,3,4,5) , a=c(1,2,3,4,5), b=c(1,2,3,4,5))
    x.1
    x.2 <- data.frame(id=c(1,2,3,4,5) , a2=c(1,1,2,3,4), b2=c(1,1,99,3,4))
    x.2
    df <- merge(x.1,x.2,by="id")
    comp <- compare(df$b,df$b2,allowAll=FALSE)
    comp
    comp$tM
1

There are 1 answers

0
CHP On BEST ANSWER

You can put your condition in standard [ function as i argument, column that you want to show as argument j and drop = F controls whether you want data.frame or vector in return in case you select only 1 column.

df[df$b != df$b2, "id", drop = F]
##   id
## 2  2
## 3  3
## 4  4
## 5  5