1

Consider you have two dataframes of the same size, consisting in entries of either TRUE, FALSE or NA:

df1 <- data.frame(a = c(T, F, F, F, T), b=c(F, NA, NA, F, T))
View(df1)

df2 <- data.frame(a = c(T, T, T, F, NA), b=c(T, NA, T, F, T))
View(df2)

Now you want to see how many entries in each row of df1 are exactly the same as in df2. The result should look like this:

SumOfSimilarValuesPerRow <- data.frame(Result = c(1, 0, 0, 2, 1))
View(SumOfSimilarValuesPerRow)

So first you probably need to compare each entry in every row of df1 with the same in df2 and then sum up the result.

I tried it with a double loop, but I keep getting the error

missing value where TRUE/FALSE needed

when trying the following:

for (i in 1:5) {
  for (j in 1:2) {
    if(df1[i, j] == df2[i, j]) {
      print("OK")
    } 
  }
}

I haven´t tried to sum up the result yet, bacause I already struggeled to compare every entry.

Does anyone know, how that would work in an easy to understand fassion?

Any help would be appreciated a lot!

2
  • Are you sure the 3rd row of your desired output is 1? Seems that there are no matches there. Commented Jun 10, 2018 at 11:53
  • @AntoniosK Yes you were right, I have edited that, thanks a lot! Commented Jun 10, 2018 at 11:59

1 Answer 1

2

You can do this simply as

rowSums(df1 == df2, na.rm = TRUE)

== does an element by element comparison, which works for the requirements of your problem because your df1 and df2 have the same sizes (and structure).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ver much, didn´t know it can be so simple! You helped me a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.