1

I have two dataframes,

A(514 rows)

1 2
2 3      
4 5
1 3
5 6
...

B(696 rows)

1 2
3 4
4 5
1 3
5 7
5 6
.....

I want to get those rows and their corresponding columns which are present in B but not in A. For example, the result will be,

3 4
5 7

How can I do it in R?

I tried using this post answers: Compare two data.frames to find the rows in data.frame 1 that are not present in data.frame 2

But here, the first answer difference doesnt give the correct output.

Community
  • 1
  • 1
sarah
  • 247
  • 1
  • 9
  • 19
  • It is a little unclear. Do you want the matching columns in B, but not the rows present in A? – akrun Sep 24 '14 at 08:59

1 Answers1

0

Try:

colIndx <- colnames(B)[colnames(B) %in% colnames(A)]
rowIndx <- !as.character(interaction(B)) %in%  as.character(interaction(A[colIndx]))
 B[rowIndx,]
#   V1 V2
#2  3  4
#5  5  7

data

 A <- structure(list(V1 = c(1L, 2L, 4L, 1L, 5L), V2 = c(2L, 3L, 5L, 
 3L, 6L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-5L))

 B <- structure(list(V1 = c(1L, 3L, 4L, 1L, 5L, 5L), V2 = c(2L, 4L, 
 5L, 3L, 7L, 6L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-6L))
akrun
  • 874,273
  • 37
  • 540
  • 662