Compare two dataframes for changes in variable value in R

chrischunli

I'm trying to compare two dataframes with the exact same amount of rows and variables, for changes in variable value per unique ID (returning True if the value is the same, and false if it is different). Here's an example of how the data looks:

df1

id col1 col2
1  abc  123
2  def  456
3  ghi  789

df2

col1 id col2
ghe  3  789
abc  1  123
def  2  455

And I guess I would have the result of the comparison be in df3

id col1 col2
1  true true
2  true false
3  false true

Any help would be greatly appreciated! Hopefully I've made this somewhat clear.

989

Try this:

cbind.data.frame(id=df1$id, df1[-1]==df2[match(df1$id, df2$id), names(df1)[-1]])

#  id  col1  col2
#1  1  TRUE  TRUE
#2  2  TRUE FALSE
#3  3 FALSE  TRUE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R compare with two dataframes by multiple conditions to extract rows

Compare two Spark dataframes

r compare column types between two dataframes

Counting the number of changes between two dataframes (row by row) in r

Compare the contingency table of two dataframes

Compare columns of two dataframes without merging the dataframes

How to compare the rows of two dataframes in R

compare two groups based on categorial variable in R

Compare two dataframes in R

Compare two dataframes Pyspark

How to compare dates from two dataframes and update the value in the column

compare columns over two dataframes

Assigning text value to variable in cypress to compare changes

Compare two columns and replace value in R

Compare and merge two dataframes

compare two variable with same value returns false

Trying to compare two dataframes, and writing a logical result to a new dataframe in R

Iterate two dataframes, compare and change a value in pandas or pyspark

compare two groups based on categorial variable in R

In R, compare string variable of two dataframes to create new flag variable indicating match in both dataframes, using a for-loop?

Compare Values Across Two Dataframes R

Compare each row in two dataframes in R

Compare dataframes and vectors in R

Compare data in two dataframes

Date compare and value changes

Merge two dataframes by a closest value in R

How to compare two dataframes and assign a conditional value

In R, compare two columns in different dataframes and delete duplicates

compare specific column values between two dataframes in R